1 block.test BlockTestCase::testCustomBlock()

Test creating custom block, editing, and then deleting it.

File

core/modules/block/tests/block.test, line 29
Tests for block.module.

Class

BlockTestCase

Code

function testCustomBlock() {
  // Confirm that the add block link appears on block overview pages.
  $this->backdropGet('admin/structure/block');
  $this->assertRaw(l('Add custom block', 'admin/structure/block/add'), 'Add block link is present on block overview page for default theme.');

  // 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();
  $file_info['attributes']['data-file-id'] = $file->fid;
  $image_string = theme('image', $file_info);

  // Load the form and check that file uploads are enabled on the body field.
  $this->backdropGet('admin/structure/block/add');
  $element = $this->xpath('//textarea[@name="body[value]"][@data-editor-uploads="true"]');
  $this->assertEqual(count($element), 1, 'Editor upload attribute found on the body field for a custom block.');

  // Add a new custom block by filling out the input form on the admin/structure/block/add page.
  $custom_block = array();
  $custom_block['info'] = $this->randomName(8);
  $custom_block['delta'] = strtolower($this->randomName(8));
  $custom_block['title'] = $this->randomName(8);
  $custom_block['body[value]'] = $this->randomName(32) . $image_string;
  $this->backdropPost(NULL, $custom_block, 'Save block');

  // Confirm that the custom block has been created, and then find its config file
  $this->assertText(t('The block has been created.'), 'Custom block successfully created.');

  // Check to see if the custom block was created by checking that it has a configuration file.
  $custom_block = config_get('block.custom.' . $custom_block['delta']);
  $this->assertNotNull($custom_block['delta'], 'Custom block found in configuration.');

  // Check that block_block_view() returns the correct title and content.
  $data = block_block_view($custom_block['delta']);
  $this->assertEqual($data['subject'], $custom_block['title'], 'block_block_view() provides the correct block title.');
  $this->assertEqual(check_markup($custom_block['body']['value'], $custom_block['body']['format']), $data['content'], 'block_block_view() provides correct block content.');

  // Check that a file usage was recorded for the file within the block.
  $file = file_load($file->fid);
  $references = file_usage_list($file);
  $this->assertEqual($references['file']['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.');

  // Check whether the block can be moved to all available regions.
  $layout = layout_load('default');
  $layout->addBlock('block', $custom_block['delta'], 'sidebar');
  $layout->save();
  $this->backdropGet('user');
  $this->assertText($data['subject'], 'Newly added block found.');

  // Verify presence of configure and delete links for custom block.
  $this->backdropGet('admin/structure/block');
  $this->assertLinkByHref('admin/structure/block/manage/' . $custom_block['delta'] . '/configure', 0, 'Custom block configure link found.');
  $this->assertLinkByHref('admin/structure/block/manage/' . $custom_block['delta'] . '/delete', 0, 'Custom block delete link found.');

  // Delete the created custom block & verify that it's been deleted and no longer appearing on the page.
  $this->clickLink(t('Delete'));
  $this->backdropPost('admin/structure/block/manage/' . $custom_block['delta'] . '/delete', array(), 'Delete');
  $this->assertRaw(t('The block %title has been removed.', array('%title' => $custom_block['info'])), 'Custom block successfully deleted.');
  $this->assertNoText(t($custom_block['title']), 'Custom block no longer appears on page.');

  // Note that file usages are NOT removed after a block has been deleted.
  // See https://github.com/backdrop/backdrop-issues/issues/2137.
}