From 84cdd9bb4bd71396745b9dee4645c7a97064a866 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Wed, 21 May 2014 21:02:29 +1200 Subject: [PATCH] MDL-44711 navigation: reverted behat chaneges --- .../tests/behat/view_my_courses.feature | 33 ----- lib/tests/behat/behat_navigation.php | 140 ++++-------------- 2 files changed, 32 insertions(+), 141 deletions(-) diff --git a/blocks/navigation/tests/behat/view_my_courses.feature b/blocks/navigation/tests/behat/view_my_courses.feature index 314d8873fed..6287bad3d15 100644 --- a/blocks/navigation/tests/behat/view_my_courses.feature +++ b/blocks/navigation/tests/behat/view_my_courses.feature @@ -69,36 +69,3 @@ Feature: View my courses in navigation block And I wait "2" seconds And I should see "c331" in the "Navigation" "block" And I should not see "c332" in the "Navigation" "block" - - @javascript - Scenario: I can expand categories and courses as guest - Given I set the following administration settings values: - | Show my course categories | 1 | - | Show all courses | 1 | - And I log out - And I expand "Courses" node - And I should see "cat1" in the "Navigation" "block" - And I should see "cat2" in the "Navigation" "block" - And I should see "cat3" in the "Navigation" "block" - And I should not see "cat31" in the "Navigation" "block" - And I should not see "cat32" in the "Navigation" "block" - And I should not see "cat331" in the "Navigation" "block" - And I should not see "c1" in the "Navigation" "block" - And I should not see "c2" in the "Navigation" "block" - And I should not see "c31" in the "Navigation" "block" - And I should not see "c32" in the "Navigation" "block" - When I expand "cat3" node - And I expand "cat31" node - And I expand "cat1" node - And I should see "c1" in the "Navigation" "block" - And I expand "c1" node - Then I should see "cat1" in the "Navigation" "block" - And I should see "cat2" in the "Navigation" "block" - And I should see "cat3" in the "Navigation" "block" - And I should see "cat31" in the "Navigation" "block" - And I should see "cat32" in the "Navigation" "block" - And I should not see "cat331" in the "Navigation" "block" - And I should see "c1" in the "Navigation" "block" - And I should not see "c2" in the "Navigation" "block" - And I should see "c31" in the "Navigation" "block" - And I should not see "c32" in the "Navigation" "block" diff --git a/lib/tests/behat/behat_navigation.php b/lib/tests/behat/behat_navigation.php index 9984de6fa81..90d68a5764c 100644 --- a/lib/tests/behat/behat_navigation.php +++ b/lib/tests/behat/behat_navigation.php @@ -40,107 +40,12 @@ use Behat\Behat\Context\Step\Given as Given, */ class behat_navigation extends behat_base { - /** - * Helper function to get a navigation nodes text element given its text from within the navigation block. - * - * This function finds the node with the given text from within the navigation block. - * It checks to make sure the node is visible, and then returns it. - * - * @param string $text - * @param bool $branch Set this true if you're only interested in the node if its a branch. - * @param null|bool $collapsed Set this to true or false if you want the node to either be collapsed or not. - * If its left as null then we don't worry about it. - * @param null|string|Exception|false $exception The exception to throw if the node is not found. - * @return \Behat\Mink\Element\NodeElement - */ - protected function get_node_text_node($text, $branch = false, $collapsed = null, $exception = null) { - if ($exception === null) { - $exception = new ExpectationException('The "' . $text . '" node could not be found', $this->getSession()); - } else if (is_string($exception)) { - $exception = new ExpectationException($exception, $this->getSession()); - } - - $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text); - $hasblocktree = "[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]"; - $hasbranch = "[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]"; - $hascollapsed = "li[contains(concat(' ', normalize-space(@class), ' '), ' collapsed ') or @data-exandable='1']"; - $notcollapsed = "li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed '))]"; - $match = "[normalize-space(.)={$nodetextliteral}]"; - - // Avoid problems with quotes. - $isbranch = ($branch) ? $hasbranch : ''; - if ($collapsed === true) { - $iscollapsed = $hascollapsed; - } else if ($collapsed === false) { - $iscollapsed = $notcollapsed; - } else { - $iscollapsed = 'li'; - } - - // First check root nodes. - $xpath = "//ul{$hasblocktree}/$hascollapsed/p{$isbranch}/span{$match}|"; - // Next search for the node containing the text within a link. - $xpath .= "//ul{$hasblocktree}//{$notcollapsed}/ul/{$iscollapsed}/p{$isbranch}/a{$match}|"; - // Finally search for the node containing the text within a span. - $xpath .= "//ul{$hasblocktree}//{$notcollapsed}/ul/{$iscollapsed}/p{$isbranch}/span{$match}"; - - $node = $this->find('xpath', $xpath, $exception); - return $node; - } - - /** - * Returns true if the navigation node with the given text is expandable. - * - * @Given /^navigation node "([^"]*)" should be expandable$/ - * - * @throws ExpectationException - * @param string $nodetext - * @return bool - */ - public function navigation_node_should_be_expandable($nodetext) { - if (!$this->running_javascript()) { - // Nodes are only expandable when JavaScript is enabled. - return false; - } - - $node = $this->get_node_text_node($nodetext, true); - $node = $node->getParent(); - if ($node->hasAttribute('data-expandable') && $node->getAttribute('data-expandable')) { - return true; - } - throw new ExpectationException('The "' . $nodetext . '" node is not expandable', $this->getSession()); - } - - /** - * Returns true if the navigation node with the given text is not expandable. - * - * @Given /^navigation node "([^"]*)" should not be expandable$/ - * - * @throws ExpectationException - * @param string $nodetext - * @return bool - */ - public function navigation_node_should_not_be_expandable($nodetext) { - if (!$this->running_javascript()) { - // Nodes are only expandable when JavaScript is enabled. - return false; - } - - $node = $this->get_node_text_node($nodetext); - $node = $node->getParent(); - if ($node->hasAttribute('data-expandable') && $node->getAttribute('data-expandable')) { - throw new ExpectationException('The "' . $nodetext . '" node is expandable', $this->getSession()); - } - return true; - } - /** * Expands the selected node of the navigation tree that matches the text. * @Given /^I expand "(?P(?:[^"]|\\")*)" node$/ * * @throws ExpectationException * @param string $nodetext - * @return bool|void */ public function i_expand_node($nodetext) { @@ -150,12 +55,23 @@ class behat_navigation extends behat_base { if (!$this->running_javascript()) { return false; } - $node = $this->get_node_text_node($nodetext, true, true, 'The "' . $nodetext . '" node can not be expanded'); - // Check if the node is a link AND a branch. - if (strtolower($node->getTagName()) === 'a') { - // We just want to expand the node, we don't want to follow it. - $node = $node->getParent(); - } + + // Avoid problems with quotes. + $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext); + + $xpath = "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" . + "/child::li[contains(concat(' ', normalize-space(@class), ' '), ' collapsed ')]" . + "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" . + "/child::span[normalize-space(.)=$nodetextliteral]" . + "|" . + "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" . + "/descendant::li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed '))]" . + "/descendant::li[contains(concat(' ', normalize-space(@class), ' '), ' collapsed ')]" . + "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" . + "/child::span[normalize-space(.)=$nodetextliteral]"; + + $exception = new ExpectationException('The "' . $nodetext . '" node can not be expanded', $this->getSession()); + $node = $this->find('xpath', $xpath, $exception); $node->click(); } @@ -165,7 +81,6 @@ class behat_navigation extends behat_base { * @Given /^I collapse "(?P(?:[^"]|\\")*)" node$/ * @throws ExpectationException * @param string $nodetext - * @return bool|void */ public function i_collapse_node($nodetext) { @@ -174,12 +89,21 @@ class behat_navigation extends behat_base { return false; } - $node = $this->get_node_text_node($nodetext, true, false, 'The "' . $nodetext . '" node can not be collapsed'); - // Check if the node is a link AND a branch. - if (strtolower($node->getTagName()) === 'a') { - // We just want to expand the node, we don't want to follow it. - $node = $node->getParent(); - } + // Avoid problems with quotes. + $nodetextliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($nodetext); + + $xpath = "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" . + "/child::li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed '))]" . + "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" . + "/child::span[normalize-space(.)=$nodetextliteral]" . + "|" . + "//ul[contains(concat(' ', normalize-space(@class), ' '), ' block_tree ')]" . + "/descendant::li[not(contains(concat(' ', normalize-space(@class), ' '), ' collapsed '))]" . + "/child::p[contains(concat(' ', normalize-space(@class), ' '), ' branch ')]" . + "/child::span[normalize-space(.)=$nodetextliteral]"; + + $exception = new ExpectationException('The "' . $nodetext . '" node can not be collapsed', $this->getSession()); + $node = $this->find('xpath', $xpath, $exception); $node->click(); } }