1 layout.test LayoutInterfaceTest::testRelationships()

Test the support for contexts within conditions and blocks.

File

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

Class

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

Code

function testRelationships() {
  // Make a new layout that creates a custom path with wildcards.
  $layout_title = $this->randomName();
  $layout_name = strtolower($layout_title);
  $layout_path = 'node/%';
  $edit = array(
    'title' => $layout_title,
    'name' => $layout_name,
    'layout_template' => 'moscone_flipped',
    'path' => $layout_path,
  );
  // Check the path first to populate available contexts.
  $this->backdropPost('admin/structure/layouts/add', $edit, t('Check path'));

  // Test Author from Content relationship.
  $edit = array();
  $this->backdropPost('admin/structure/layouts/manage/' . $layout_name . '/configure', $edit, t('Add relationship'));

  $edit = array(
    'relationship' => 'author_from_node:relationship',
  );
  $this->backdropPost(NULL, $edit, t('Load relationship'));
  $this->backdropPost(NULL, array(), t('Add relationship'));

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

  // Add the test block that requires both node and user contexts.
  $this->clickLink(t('Add block'), 3);
  $this->assertText(t('A testing block for layouts with contexts.'));
  $this->clickLink(t('Layout bar block'));
  $select_options = $this->xpath('//*[@id=:select]//option', array(
    ':select' => 'edit-contexts-my-user',
  ));
  $this->assertEqual(count($select_options), 2, 'Two options are found in the select list.');
  $relationship_option = (string) $select_options[0]['value'];
  $this->assertTrue(strpos($relationship_option, 'relationship') === 0, 'First select list option is the user context from the author relationship.');

  $edit = array(
    'contexts[my_user]' => $relationship_option,
  );
  $this->backdropPost(NULL, $edit, t('Add block'));

  // Record the UUID for the newly added block.
  $last_block = $this->xpath('(//*[contains(@class,:region)]//*[@data-block-id])[last()]', array(
    ':region' => 'l-sidebar',
  ));
  $block_uuid = (string) $last_block[0]['data-block-id'];
  $block_edit_url = 'admin/structure/layouts/manage/' . $layout_name . '/configure-block/editor/' . $block_uuid;

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

  // Visit a node created by another user. The user context from that node,
  // rather than the current user, should be available.
  // The current user is User 2.
  $this->backdropGet('node/' . $this->test_node2->nid);
  $this->assertText(format_string('The user email is @mail and the node title is @title', array('@mail' => $this->web_user->mail, '@title' => $this->test_node2->title)));
  $this->backdropGet('admin/structure/layouts/manage/' . $layout_name);

  // Change the user context on the test block to use the current user and
  // test that the node user is no longer displayed.
  $edit = array(
    'contexts[my_user]' => 'current_user',
  );
  $this->backdropPost($block_edit_url, $edit, t('Update block'));
  $this->backdropPost(NULL, array(), t('Save layout'));

  $this->backdropGet('node/' . $this->test_node2->nid);
  $this->assertNoText(format_string('The user email is @mail and the node title is @title', array('@mail' => $this->web_user->mail, '@title' => $this->test_node2->title)));

  // Visit a node created by the current user (User 2) and test that the
  // correct user is displayed.
  $this->backdropGet('node/' . $this->test_node1->nid);
  $this->assertText(format_string('The user email is @mail and the node title is @title', array('@mail' => $this->admin_user->mail, '@title' => $this->test_node1->title)));

  // Remove the author from node relationship.
  $this->backdropPost('admin/structure/layouts/manage/' . $layout_name . '/configure', array(), t('Remove'));
}