1 layout.test LayoutBlockTextTest::testBlockText()

Tests the BlockText class functionality.

File

core/modules/layout/tests/layout.test, line 2681
Tests for the Layout module.

Class

LayoutBlockTextTest
Tests the BlockTest (custom text) block functionality.

Code

function testBlockText() {
  // Save a file to test file usage saving.
  $files = $this->backdropGetTestFiles('image');
  $file_info = (array) array_pop($files);
  $file_info['status'] = 0;
  $file = new File($file_info);
  $file->save();
  $fid = $file->fid;
  $file_info['attributes']['data-file-id'] = $fid;
  $image_string = theme('image', $file_info);

  $this->backdropGet('admin/structure/layouts/manage/default/add-block/editor/sidebar');
  $this->clickLink(t('Custom block'));
  $this->backdropPost(NULL, array(
    'title' => 'Custom block test',
    'content[value]' => '<p>Some content with an image in the middle.</p><p>' . $image_string . '</p><p>End of content.</p>',
  ), t('Add block'));

  // Get the block UUID.
  $last_block = $this->xpath('(//*[contains(@class,:region)]//*[@data-block-id])[last()]', array(
    ':region' => 'l-sidebar',
  ));
  $block_uuid = (string) $last_block[0]['data-block-id'];

  // Check that file usage is not added by the act of adding the block, as the
  // layout has not yet been saved.
  $usage = file_usage_list($file);
  $this->assertEqual($usage, array(), 'No file usages recorded (as correct) when adding block to layout.');

  $this->backdropPost(NULL, array(), t('Save layout'));

  // Check that a file usage was recorded for the file within the block.
  entity_get_controller('file')->resetCache();
  $file = file_load($fid);
  $references = file_usage_list($file);
  $this->assertEqual($references['file']['file'][$fid], 1, 'File usage recorded for the file within the block.');
  $this->assertEqual($file->status, 1, 'File has been marked permanent by its file usage.');

  // Visit the front-end and check the block is displayed.
  $this->backdropGet('user');
  $this->assertRaw('Custom block test', 'Custom block found on front-end.');

  // Removing the block.
  $this->backdropGet('admin/structure/layouts/manage/default');
  $remove_link = $this->xpath('//*[@data-block-id=:uuid]//a[contains(@class,"remove-block")]', array(
    ':uuid' => $block_uuid,
  ));
  $remove_url_parts = backdrop_parse_url($remove_link[0]['href']);
  $this->backdropGet($remove_url_parts['path'], $remove_url_parts);
  $this->backdropPost(NULL, array(), t('Save layout'));

  // Check the file usage is left in place.
  entity_get_controller('file')->resetCache();
  $file = file_load($fid);
  $references = file_usage_list($file);
  $this->assertEqual($references['file']['file'][$fid], 1, 'File usage is unaffected by removing the block from the layout.');
  $this->assertEqual($file->status, 1, 'File still exists and is marked as permanent after removing the block from the layout.');
}