- <?php
- * @file
- * Tests for the Administration bar module.
- */
-
- * Base class for all administration bar web test cases.
- */
- class AdminBarWebTestCase extends BackdropWebTestCase {
- protected $profile = 'testing';
-
- protected $basePermissions = array(
- 'system' => 'access administration pages',
- 'admin_bar' => 'access administration bar',
- 'node' => 'access content',
- );
-
- function setUp() {
-
- $modules = func_get_args();
- $modules = isset($modules[0]) ? $modules[0] : $modules;
- $modules[] = 'admin_bar';
- $modules[] = 'node';
- parent::setUp($modules);
- }
-
-
- * Check that an element exists in HTML markup.
- *
- * @param $xpath
- * An XPath expression.
- * @param array $arguments
- * (optional) An associative array of XPath replacement tokens to pass to
- * BackdropWebTestCase::buildXPathQuery().
- * @param $message
- * The message to display along with the assertion.
- * @param $group
- * The type of assertion - examples are "Browser", "PHP".
- *
- * @return
- * TRUE if the assertion succeeded, FALSE otherwise.
- */
- protected function assertElementByXPath($xpath, array $arguments, $message, $group = 'Other') {
- $elements = $this->xpath($xpath, $arguments);
- return $this->assertTrue(!empty($elements[0]), $message, $group);
- }
-
-
- * Check that an element does not exist in HTML markup.
- *
- * @param $xpath
- * An XPath expression.
- * @param array $arguments
- * (optional) An associative array of XPath replacement tokens to pass to
- * BackdropWebTestCase::buildXPathQuery().
- * @param $message
- * The message to display along with the assertion.
- * @param $group
- * The type of assertion - examples are "Browser", "PHP".
- *
- * @return
- * TRUE if the assertion succeeded, FALSE otherwise.
- */
- protected function assertNoElementByXPath($xpath, array $arguments, $message, $group = 'Other') {
- $elements = $this->xpath($xpath, $arguments);
- return $this->assertTrue(empty($elements), $message, $group);
- }
-
-
- * Asserts that links appear in the menu in a specified trail.
- *
- * @param array $trail
- * A list of menu link titles to assert in the menu.
- */
- protected function assertLinkTrailByTitle(array $trail) {
- list($xpath, $args, $message) = $this->_getXpathLinkTrailByTitle($trail);
- $this->assertElementByXPath($xpath, $args, $message . ' link found.');
- }
-
-
- * Asserts that no link appears in the menu for a specified trail.
- *
- * @param array $trail
- * A list of menu link titles to assert in the menu.
- */
- protected function assertNoLinkTrailByTitle(array $trail) {
- list($xpath, $args, $message) = $this->_getXpathLinkTrailByTitle($trail);
- $this->assertNoElementByXPath($xpath, $args, $message . ' link not found.');
- }
-
-
- * Shared logic for assertLinkTrailByTitle() and assertNoLinkTrailByTitle().
- *
- * @param array $trail
- * A list of menu link titles to assert in the menu.
- *
- * @return array
- * Unindexed array with the following:
- * - The xpath selector for the link.
- * - The arguments to pass to the xpath selector.
- * - The link trail represented as text.
- */
- private function _getXpathLinkTrailByTitle(array $trail) {
- $xpath = array();
- $args = array();
- $message = '';
- foreach ($trail as $i => $link_text) {
- $xpath[] = '/li/a/span[text()=:text' . $i . ']';
- $args[':text' . $i] = $link_text;
- $message .= ($i ? ' » ' : '') . check_plain($link_text);
- }
- return array(
- '//div[@id="admin-bar"]/div/ul/li/ul' . implode('/ancestor::li/ul', $xpath),
- $args,
- $message,
- );
- }
- }
-
- * Tests menu links depending on user permissions.
- */
- class AdminBarPermissionsTestCase extends AdminBarWebTestCase {
- function setUp() {
- parent::setUp(array('node'));
- }
-
-
- * Test that the links are added to the page (no JS testing).
- */
- function testPermissions() {
- module_enable(array('contact'));
- $this->resetAll();
-
-
- $this->backdropGet('');
- $this->assertNoElementByXPath('//div[@id="admin-bar"]', array(), t('Administration bar not found.'));
-
-
-
-
- $permissions = $this->basePermissions + array(
- 'access content overview',
- );
- $admin_user = $this->backdropCreateUser($permissions);
- $this->backdropLogin($admin_user);
-
-
- $this->assertElementByXPath('//div[@id="admin-bar"]', array(), 'Administration bar found.');
- $this->assertElementByXPath('//div[@id="admin-bar"]//a[contains(@href, :path)]', array(':path' => 'admin/content'), 'Content link found.');
- $this->assertNoElementByXPath('//div[@id="admin-bar"]//a[contains(@href, :path)]', array(':path' => 'admin/structure/contact'), 'Structure » Contact link not found.');
-
-
-
-
- $permissions = $this->basePermissions + array(
- 'administer contact forms',
- );
- $admin_user2 = $this->backdropCreateUser($permissions);
- $this->backdropLogin($admin_user2);
- $this->assertElementByXPath('//div[@id="admin-bar"]', array(), 'Administration bar found.');
- $this->assertNoElementByXPath('//div[@id="admin-bar"]//a[contains(@href, :path)]', array(':path' => 'admin/content'), 'Content link not found.');
- $this->assertElementByXPath('//div[@id="admin-bar"]//a[contains(@href, :path)]', array(':path' => 'admin/structure/contact'), 'Structure » Contact link found.');
- }
-
-
- * Tests handling of links pointing to category/overview pages.
- */
- function testCategories() {
-
- $admin_user = $this->backdropCreateUser($this->basePermissions);
- $this->backdropLogin($admin_user);
-
-
- $this->assertNoLinkTrailByTitle(array(t('Structure')));
- $this->assertNoLinkTrailByTitle(array(t('Configuration')));
-
-
- $permissions = $this->basePermissions + array(
- 'administer users',
- 'administer account settings',
- );
- $admin_user = $this->backdropCreateUser($permissions);
- $this->backdropLogin($admin_user);
-
-
- $this->assertNoLinkTrailByTitle(array(t('Structure')));
- $this->assertLinkTrailByTitle(array(t('User accounts')));
- $this->assertLinkTrailByTitle(array(t('Configuration')));
- $this->assertLinkTrailByTitle(array(t('Configuration'), t('User accounts')));
-
- $this->assertNoLinkTrailByTitle(array(t('Configuration'), t('Media')));
- $this->assertNoLinkTrailByTitle(array(t('Configuration'), t('System')));
- }
-
-
- * Tests that user role and permission changes are properly taken up.
- */
- function testPermissionChanges() {
-
- $permissions = $this->basePermissions + array(
- 'administer permissions',
- 'assign roles',
- );
- $admin_user = $this->backdropCreateUser($permissions);
- $this->backdropLogin($admin_user);
-
-
- $roles = array_diff($admin_user->roles, array(BACKDROP_AUTHENTICATED_ROLE));
- $role_name = reset($roles);
-
-
- $this->assertNoLinkTrailByTitle(array(t('Configuration'), t('System')));
-
- $edit = array(
- $role_name . '[administer site configuration]' => TRUE,
- );
- $this->backdropPost('admin/config/people/permissions', $edit, t('Save permissions'));
-
- $this->assertLinkTrailByTitle(array(t('Configuration'), t('System')));
-
-
- $this->assertNoLinkTrailByTitle(array(t('Structure'), t('Content types')));
-
- $edit = array(
- 'name' => 'test',
- 'label' => 'Test',
- );
- $this->backdropPost('admin/config/people/roles/add', $edit, t('Save role'));
-
-
- $edit = array(
- 'test[administer content types]' => TRUE,
- );
- $this->backdropPost('admin/config/people/permissions/test', $edit, t('Save permissions'));
-
- $this->assertNoLinkTrailByTitle(array(t('Structure'), t('Content types')));
-
-
- $edit = array(
- 'roles[test]' => TRUE,
- );
- $this->backdropPost('user/' . $admin_user->uid . '/edit', $edit, t('Save'));
-
- $this->assertLinkTrailByTitle(array(t('Structure'), t('Content types')));
-
-
- $this->backdropPost('admin/config/people/roles/delete/test', array(), t('Delete'));
-
- $this->assertNoLinkTrailByTitle(array(t('Structure'), t('Content types')));
- }
- }
-
- * Tests appearance, localization, and escaping of dynamic links.
- */
- class AdminBarDynamicLinksTestCase extends AdminBarWebTestCase {
-
-
- * @var User
- */
- protected $admin_user;
-
-
- * @var User
- */
- protected $web_user;
-
- function setUp() {
- parent::setUp(array('node'));
- }
-
-
- * Tests node type links.
- */
- function testNode() {
- $this->backdropCreateContentType(array('type' => 'post', 'name' => 'Post'));
-
- $this->backdropCreateContentType(array('type' => 'special', 'name' => 'Cool & Special'));
-
- $permissions = $this->basePermissions + array(
- 'administer content types',
- 'create post content',
- 'create special content',
- );
- $this->admin_user = $this->backdropCreateUser($permissions);
- $this->backdropLogin($this->admin_user);
-
-
- $cid = 'admin_bar:' . $this->admin_user->uid . ':' . session_id() . ':en';
- $hash = admin_bar_cache_get($cid);
- $this->backdropGet('js/admin_bar/cache/' . $hash);
- $this->assertElementByXPath('//div[@id="admin-bar"]', array(), t('Administration bar found.'));
- $this->assertElementByXPath('//div[@id="admin-bar"]//a[contains(@href, :path)]', array(':path' => 'admin/structure/types'), "Structure » Content types link found.");
-
-
- $this->assertNoRaw('Cool & Special');
- $this->assertRaw(check_plain('Cool & Special'));
-
-
- $links = array(
- 'admin/structure/types/manage/post' => 'Post',
- 'admin/structure/types/manage/special' => 'Cool & Special',
- );
- foreach ($links as $path => $title) {
- $this->assertElementByXPath('//div[@id="admin-bar"]//a[contains(@href, :path)]//*[contains(text(), :text)]', array(
- ':path' => $path,
- ':text' => $title,
- ), "Structure » Content types » $title link found.");
- }
-
-
- $links = array(
- 'node/add/post' => 'Post',
- 'node/add/special' => 'Cool & Special',
- );
- foreach ($links as $path => $title) {
- $this->assertElementByXPath('//div[@id="admin-bar"]//a[contains(@href, :path)]//*[contains(text(), :text)]', array(
- ':path' => $path,
- ':text' => $title,
- ), "Add content » $title link found.");
- }
-
-
-
- module_load_include('inc', 'admin_bar');
- $link = db_query("SELECT * FROM {menu_links} WHERE router_path LIKE 'node/add%' AND module = 'system'")->fetchAssoc();
- $link = menu_link_load($link['mlid']);
- $link['menu_name'] = 'main-menu';
- menu_link_save($link);
-
-
- $this->backdropPost('admin/structure/types/manage/special/delete', array(), t('Delete'));
- $this->backdropGet('js/admin_bar/cache/' . $hash);
-
- $this->assertElementByXPath('//div[@id="admin-bar"]//a[contains(@href, :path)]//*[contains(text(), :text)]', array(
- ':path' => 'node/add/post',
- ':text' => 'Post',
- ), "Add content » Post link found after moving Add Content into a different menu.");
- $this->assertNoElementByXPath('//div[@id="admin-bar"]//a[contains(@href, :path)]//*[contains(text(), :text)]', array(
- ':path' => 'node/add/special',
- ':text' => 'Cool & Special',
- ), "Add content » Cool & Special link no longer present after deleting the content type.");
- }
-
-
- * Tests Add content links.
- */
- function testNodeAdd() {
- $this->backdropCreateContentType(array('type' => 'post', 'name' => 'Post'));
-
-
- $permissions = $this->basePermissions + array();
- $this->web_user = $this->backdropCreateUser($permissions);
- $this->backdropLogin($this->web_user);
- $this->assertNoText(t('Add content'));
-
-
- $permissions = $this->basePermissions + array(
- 'access content overview',
- 'create post content',
- );
- $this->admin_user = $this->backdropCreateUser($permissions);
- $this->backdropLogin($this->admin_user);
- $this->assertLinkTrailByTitle(array(
- t('Content'),
- t('Add content'),
- ));
-
-
- $permissions = $this->basePermissions + array(
- 'create post content',
- );
- $this->web_user = $this->backdropCreateUser($permissions);
- $this->backdropLogin($this->web_user);
- $this->assertLinkTrailByTitle(array(
- t('Add content'),
- ));
- }
- }
-
- * Tests appearance of different types of links.
- */
- class AdminBarLinkTypesTestCase extends AdminBarWebTestCase {
-
-
- * @var User
- */
- protected $admin_user;
-
- function setUp() {
- parent::setUp();
-
- $permissions = module_invoke_all('permission');
- $permissions = array_keys($permissions);
- $this->admin_user = $this->backdropCreateUser($permissions);
- $this->backdropLogin($this->admin_user);
- }
-
-
- * Tests appearance of different router item link types.
- */
- function testLinkTypes() {
-
- $this->assertLinkTrailByTitle(array(
- t('Configuration'),
- t('System'),
- t('Site information'),
- ));
-
-
- $this->assertLinkTrailByTitle(array(t('Functionality'), t('Uninstall modules')));
-
-
- $this->assertLinkTrailByTitle(array(
- t('User accounts'),
- t('Add user account'),
- ));
-
-
- $this->assertNoLinkTrailByTitle(array(t('Appearance'), t('List')));
- $this->assertNoLinkTrailByTitle(array(t('Modules'), t('List')));
-
-
- $this->assertNoLinkTrailByTitle(array(t('Modules'), t('Uninstall'), t('Uninstall')));
- }
- }
-
- * Tests customized menu links.
- */
- class AdminBarCustomizedTestCase extends AdminBarWebTestCase {
-
-
- * @var User
- */
- protected $admin_user;
-
- function setUp() {
- parent::setUp(array('menu'));
-
- $this->admin_user = $this->backdropCreateUser($this->basePermissions + array(
- 'administer menu',
- ));
- $this->backdropLogin($this->admin_user);
- }
-
-
- * Test disabled custom links.
- */
- function testCustomDisabled() {
- $type = $this->backdropCreateContentType();
- $node = $this->backdropCreateNode(array('type' => $type->type));
- $text = $this->randomName();
- $xpath = $this->buildXPathQuery('//div[@id=:id]//span[contains(@class, "admin-bar-link-text") and contains(text(), :text)]', array(
- ':id' => 'admin-bar',
- ':text' => $text,
- ));
-
-
- $this->backdropGet('admin/config/administration/admin_bar');
- $elements = $this->xpath($xpath);
- $this->assertFalse($elements, 'Custom link not found.');
-
-
- $edit = array(
- 'link_path' => 'node/' . $node->nid,
- 'link_title' => $text,
- 'parent' => 'management:' . $this->queryMlidByPath('admin'),
- );
- $this->backdropPost('admin/structure/menu/manage/management/add', $edit, t('Save'));
-
-
- $cid = 'admin_bar:' . $this->admin_user->uid . ':' . session_id() . ':en';
- $hash = admin_bar_cache_get($cid);
- $this->backdropGet('js/admin_bar/cache/' . $hash);
- $elements = $this->xpath($xpath);
- $this->assertTrue($elements, 'Custom link found.');
-
-
- $edit = array(
- 'enabled' => FALSE,
- );
- $this->backdropPost('admin/structure/menu/item/' . $this->queryMlidByPath('node/' . $node->nid) . '/edit', $edit, t('Save'));
-
-
- $this->backdropGet('admin/config/administration/admin_bar');
- $elements = $this->xpath($xpath);
- $this->assertFalse($elements, 'Disabled custom link not found.');
- }
-
-
- * Tests external links.
- */
- function testCustomExternal() {
-
- $edit = array(
- 'link_path' => 'http://example.com',
- 'link_title' => 'Example',
- 'parent' => 'management:' . $this->queryMlidByPath('admin'),
- );
- $this->backdropPost('admin/structure/menu/manage/management/add', $edit, t('Save'));
-
-
- $cid = 'admin_bar:' . $this->admin_user->uid . ':' . session_id() . ':en';
- $hash = admin_bar_cache_get($cid);
- $this->backdropGet('js/admin_bar/cache/' . $hash);
- $elements = $this->xpath('//div[@id=:id]//a[@href=:href]//*[contains(text(), :text)]', array(
- ':id' => 'admin-bar',
- ':href' => $edit['link_path'],
- ':text' => $edit['link_title'],
- ));
- $this->assertTrue($elements, 'External link found.');
- }
-
-
- * Returns the menu link ID for a given link path in the management menu.
- */
- protected function queryMlidByPath($path) {
- return db_query('SELECT mlid FROM {menu_links} WHERE menu_name = :menu AND link_path = :path', array(
- ':menu' => 'management',
- ':path' => $path,
- ))->fetchField();
- }
- }