public function CustomBlockSaveTest::testImport

Checks whether custom block IDs are saved properly during an import.

File

drupal/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockSaveTest.php, line 47
Contains \Drupal\custom_block\Tests\CustomBlockSaveTest.

Class

CustomBlockSaveTest
Tests block save related functionality.

Namespace

Drupal\custom_block\Tests

Code

public function testImport() {

  // Custom block ID must be a number that is not in the database.
  $max_id = db_query('SELECT MAX(id) FROM {custom_block}')
    ->fetchField();
  $test_id = $max_id + mt_rand(1000, 1000000);
  $info = $this
    ->randomName(8);
  $block = array(
    'info' => $info,
    'block_body' => array(
      Language::LANGCODE_NOT_SPECIFIED => array(
        array(
          'value' => $this
            ->randomName(32),
        ),
      ),
    ),
    'type' => 'basic',
    'id' => $test_id,
  );
  $block = entity_create('custom_block', $block);
  $block
    ->enforceIsNew(TRUE);
  $block
    ->save();

  // Verify that block_submit did not wipe the provided id.
  $this
    ->assertEqual($block->id->value, $test_id, 'Block imported using provide id');

  // Test the import saved.
  $block_by_id = custom_block_load($test_id);
  $this
    ->assertTrue($block_by_id, 'Custom block load by block ID.');
}