1 layout.test LayoutInterfaceTest::testBlockPathTypeConditions()

Test block path and type conditions.

File

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

Class

LayoutInterfaceTest
Tests the interface for adding, removing, and moving blocks.

Code

function testBlockPathTypeConditions() {
  // Create a new layout overriding node/%.
  $layout_title = $this->randomName();
  $layout_name = strtolower($layout_title);
  $layout_url = 'node/%';
  $edit = array(
    'title' => $layout_title,
    'name' => $layout_name,
    'layout_template' => 'moscone_flipped',
    'path' => $layout_url,
  );
  $this->backdropPost('admin/structure/layouts/add', $edit, t('Create layout'));

  // Add a testing block, shown only on page nodes.
  $this->clickLink(t('Add block'), 3);
  $this->clickLink(t('Layout foo block'));
  $block_title = $this->randomString();
  $edit = array(
    'title_display' => LAYOUT_TITLE_CUSTOM,
    'title' => $block_title,
  );
  $this->backdropPost(NULL, $edit, t('Add condition'));
  $edit = array(
    'condition' => 'node_type',
  );
  $this->backdropPost(NULL, $edit, t('Load condition'));
  $edit = array(
    'bundles[page]' => TRUE,
  );
  $this->backdropPost(NULL, $edit, t('Add condition'));
  $this->backdropPost(NULL, array(), t('Update block'));
  $this->backdropPost(NULL, array(), t('Save layout'));

  $this->backdropGet('node/' . $this->test_node1->nid);
  $this->assertNoText(check_plain($block_title), 'Block not shown on post content.');

  $this->backdropGet('node/' . $this->test_node2->nid);
  $this->assertText(check_plain($block_title), 'Block shown on page content.');

  // Get the block UUID.
  layout_reset_caches();
  $layout = layout_load($layout_name);
  $block_uuid = end($layout->positions['sidebar']);

  // Edit condition to show this block only on post nodes.
  $this->backdropGet('admin/structure/layouts/manage/' . $layout_name . '/configure-block/editor/' . $block_uuid);
  $this->backdropPost(NULL, array(), t('Configure'));
  $this->assertRaw('<label for="edit-bundles">Node Type </label>');

  $edit = array(
    'condition' => 'node_type',
  );
  $this->backdropPost(NULL, $edit, t('Load condition'));
  $edit = array(
    'bundles[post]' => TRUE,
    'bundles[page]' => FALSE,
  );
  $this->backdropPost(NULL, $edit, t('Save visibility condition'));
  $this->backdropPost(NULL, array(), t('Update block'));
  $this->backdropPost(NULL, array(), t('Save layout'));

  $this->backdropGet('node/' . $this->test_node1->nid);
  $this->assertText(check_plain($block_title), 'Block shown on post content.');

  $this->backdropGet('node/' . $this->test_node2->nid);
  $this->assertNoText(check_plain($block_title), 'Block not shown on page content.');

  // Add another condition to show this block only on a single node path.
  $this->backdropGet('admin/structure/layouts/manage/' . $layout_name . '/configure-block/editor/' . $block_uuid);
  $this->backdropPost(NULL, array(), t('Add condition'));
  $edit = array(
    'condition' => 'path',
  );
  $this->backdropPost(NULL, $edit, t('Load condition'));
  $edit = array(
    'paths' => 'node/' . $this->test_node1->nid,
  );
  $this->backdropPost(NULL, $edit, t('Add condition'));
  $this->backdropPost(NULL, array(), t('Update block'));
  $this->backdropPost(NULL, array(), t('Save layout'));

  // Create another post node and check the combination of conditions work.
  $second_post = $this->backdropCreateNode(array(
    'type' => 'post',
    'title' => $this->randomString(),
  ));
  $this->backdropGet('node/' . $this->test_node1->nid);
  $this->assertText(check_plain($block_title), 'Block shown on post content and path matches.');

  $this->backdropGet('node/' . $second_post->nid);
  $this->assertNoText(check_plain($block_title), 'Block not shown on post content when path does not match.');
}