1 field.test FieldBlockTestCase::testFieldBlock()

Check special conditions around the main content block.

File

core/modules/field/tests/field.test, line 3544
Tests for field.module.

Class

FieldBlockTestCase
Test the field blocks and their display of different formatters.

Code

function testFieldBlock() {
  $this->backdropLogin($this->admin_user);

  // Override the node/% path to test the availability of the field blocks.
  $layout_path = 'node/%';
  $edit = array(
    'title' => 'Post layout',
    'name' => 'node_post',
    'layout_template' => 'moscone_flipped',
    'path' => $layout_path,
  );
  // Save directly without checking contexts.
  $this->backdropPost('admin/structure/layouts/add', $edit, t('Create layout'));

  // Add the field block to the right sidebar.
  $this->clickLink(t('Add block'), 3);
  $this->clickLink(t('Field: @label (@name)', array('@label' => $this->instance['label'], '@name' => $this->field_name)));

  $block_settings = array(
    'formatter' => 'field_test_default',
    'formatter_settings[test_formatter_setting]' => 'My custom formatter string',
  );

  $this->backdropPost(NULL, $block_settings, t('Add block'));

  // Check that the formatter summary is displayed.
  $this->assertText('test_formatter_setting: My custom formatter string');

  // Record the UUID for the newly added block.
  $last_block = $this->xpath('(//*[@id="layout-edit-main"]//*[contains(@class,:region)]//*[@data-block-id])[last()]', array(
    ':region' => 'l-sidebar',
  ));
  $block_uuid = (string) $last_block[0]['data-block-id'];

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

  // Check the block contents display with the configured formatter.
  $this->backdropGet('node/' . $this->node->nid);
  $block_class = backdrop_clean_css_identifier('block-field-node-' . $this->field_name);
  $frontend_block = $this->xpath('(//*[contains(@class, :region)]//*[contains(@class, :block)])[last()]', array(
    ':region' => 'l-sidebar',
    ':block' => $block_class,
  ));

  // The ".//" in these selectors means "any element under the starting item".
  $block_title = ($frontend_block[0]->xpath('.//h2'));
  $field_items = ($frontend_block[0]->xpath('.//*[contains(@class, "field-items")]/*[contains(@class, "field-item")]'));
  $this->assertIdentical((string) $block_title[0], $this->instance['label'], 'Block title displayed as field label.');
  $this->assertIdentical((string) $field_items[0], 'My custom formatter string|1', 'First item displayed with correct formatter settings.');
  $this->assertIdentical((string) $field_items[1], 'My custom formatter string|2', 'Second item displayed with correct formatter settings.');

  // Edit the block and change the title display settings.
  $this->backdropGet('admin/structure/layouts/manage/node_post/configure-block/editor/' . $block_uuid);
  $block_settings = array(
    'title_display' => 'none',
    'label' => 'above',
  );
  $this->backdropPost(NULL, $block_settings, t('Update block'));
  $this->backdropPost(NULL, array(), t('Save layout'));

  // Check the block title settings are shown on the front-end.
  $this->backdropGet('node/' . $this->node->nid);
  $frontend_block = $this->xpath('(//*[contains(@class, :region)]//*[contains(@class, :block)])[last()]', array(
    ':region' => 'l-sidebar',
    ':block' => $block_class,
  ));

  // The ".//" in these selectors means "any element under the starting item".
  $block_title = ($frontend_block[0]->xpath('.//h2'));
  $field_label = ($frontend_block[0]->xpath('.//*[contains(@class, "field-label-above")]/*[contains(@class, "field-label")]'));
  $this->assertIdentical(array(), $block_title, 'Block title not shown when set to "None".');
  $this->assertTrue(strpos($field_label[0], $this->instance['label']) !== FALSE, 'Block label shown above the field content in the block.');
}