MDL-58428 theme: Shift templates ready for Bootstrapbase removal
[moodle.git] / blocks / tests / behat / behat_blocks.php
CommitLineData
fe856690
DM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Steps definitions related with blocks.
19 *
abe572e3 20 * @package core_block
fe856690
DM
21 * @category test
22 * @copyright 2012 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27
ebcff7e2
MG
28use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
29
fe856690
DM
30require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
31
fe856690
DM
32/**
33 * Blocks management steps definitions.
34 *
abe572e3 35 * @package core_block
fe856690
DM
36 * @category test
37 * @copyright 2012 David MonllaĆ³
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class behat_blocks extends behat_base {
41
42 /**
43 * Adds the selected block. Editing mode must be previously enabled.
44 *
45 * @Given /^I add the "(?P<block_name_string>(?:[^"]|\\")*)" block$/
46 * @param string $blockname
47 */
48 public function i_add_the_block($blockname) {
e3652936
MM
49 $addblock = get_string('addblock');
50 $this->execute('behat_navigation::i_select_from_flat_navigation_drawer', $addblock);
1c00d6f6 51
1c00d6f6 52 if (!$this->running_javascript()) {
e3652936
MM
53 $this->execute('behat_general::i_click_on_in_the', [$blockname, 'link_exact', '#region-main', 'css_element']);
54 } else {
55 $this->execute('behat_general::i_click_on_in_the', [$blockname, 'link_exact', $addblock, 'dialogue']);
1c00d6f6 56 }
fe856690
DM
57 }
58
5d2b6722 59 /**
ebcff7e2
MG
60 * Adds the selected block if it is not already present. Editing mode must be previously enabled.
61 *
62 * @Given /^I add the "(?P<block_name_string>(?:[^"]|\\")*)" block if not present$/
63 * @param string $blockname
64 */
65 public function i_add_the_block_if_not_present($blockname) {
66 try {
67 $this->get_text_selector_node('block', $blockname);
68 } catch (ElementNotFoundException $e) {
69 $this->execute('behat_blocks::i_add_the_block', [$blockname]);
70 }
71 }
72
73 /**
5d2b6722
DM
74 * Docks a block. Editing mode should be previously enabled.
75 *
76 * @Given /^I dock "(?P<block_name_string>(?:[^"]|\\")*)" block$/
77 * @param string $blockname
5d2b6722
DM
78 */
79 public function i_dock_block($blockname) {
80
81 // Looking for both title and alt.
82 $xpath = "//input[@type='image'][@title='" . get_string('dockblock', 'block', $blockname) . "' or @alt='" . get_string('addtodock', 'block') . "']";
eb9ca848
RT
83 $this->execute('behat_general::i_click_on_in_the',
84 array($xpath, "xpath_element", $this->escape($blockname), "block")
85 );
5d2b6722
DM
86 }
87
b100c4e3
AN
88 /**
89 * Opens a block's actions menu if it is not already opened.
90 *
91 * @Given /^I open the "(?P<block_name_string>(?:[^"]|\\")*)" blocks action menu$/
92 * @throws DriverException The step is not available when Javascript is disabled
93 * @param string $blockname
b100c4e3
AN
94 */
95 public function i_open_the_blocks_action_menu($blockname) {
96
97 if (!$this->running_javascript()) {
3ee51425
MG
98 // Action menu does not need to be open if Javascript is off.
99 return;
b100c4e3
AN
100 }
101
102 // If it is already opened we do nothing.
3ee51425
MG
103 $blocknode = $this->get_text_selector_node('block', $blockname);
104 if ($blocknode->hasClass('action-menu-shown')) {
b100c4e3
AN
105 return;
106 }
107
eb9ca848 108 $this->execute('behat_general::i_click_on_in_the',
e3652936 109 array("a[data-toggle='dropdown']", "css_element", $this->escape($blockname), "block")
eb9ca848 110 );
b100c4e3
AN
111 }
112
113 /**
3ee51425
MG
114 * Clicks on Configure block for specified block. Page must be in editing mode.
115 *
116 * Argument block_name may be either the name of the block or CSS class of the block.
b100c4e3 117 *
3ee51425
MG
118 * @Given /^I configure the "(?P<block_name_string>(?:[^"]|\\")*)" block$/
119 * @param string $blockname
b100c4e3 120 */
3ee51425
MG
121 public function i_configure_the_block($blockname) {
122 // Note that since $blockname may be either block name or CSS class, we can not use the exact label of "Configure" link.
eb9ca848
RT
123
124 $this->execute("behat_blocks::i_open_the_blocks_action_menu", $this->escape($blockname));
125
126 $this->execute('behat_general::i_click_on_in_the',
127 array("Configure", "link", $this->escape($blockname), "block")
3ee51425 128 );
b100c4e3 129 }
392367c4
MG
130
131 /**
132 * Ensures that block can be added to the page but does not actually add it.
133 *
134 * @Then /^the add block selector should contain "(?P<block_name_string>(?:[^"]|\\")*)" block$/
135 * @param string $blockname
136 */
137 public function the_add_block_selector_should_contain_block($blockname) {
e3652936
MM
138 $addblock = get_string('addblock');
139 $this->execute('behat_navigation::i_select_from_flat_navigation_drawer', $addblock);
140
141 $cancelstr = get_string('cancel');
142 if (!$this->running_javascript()) {
143 $this->execute('behat_general::should_exist_in_the', [$blockname, 'link_exact', '#region-main', 'css_element']);
144 $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link_exact', '#region-main', 'css_element']);
145 } else {
146 $this->execute('behat_general::should_exist_in_the', [$blockname, 'link_exact', $addblock, 'dialogue']);
147 $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'button', $addblock, 'dialogue']);
148 }
392367c4
MG
149 }
150
151 /**
152 * Ensures that block can not be added to the page.
153 *
154 * @Then /^the add block selector should not contain "(?P<block_name_string>(?:[^"]|\\")*)" block$/
155 * @param string $blockname
156 */
157 public function the_add_block_selector_should_not_contain_block($blockname) {
e3652936
MM
158 $addblock = get_string('addblock');
159 $this->execute('behat_navigation::i_select_from_flat_navigation_drawer', $addblock);
160
161 $cancelstr = get_string('cancel');
162 if (!$this->running_javascript()) {
163 $this->execute('behat_general::should_not_exist_in_the', [$blockname, 'link_exact', '#region-main', 'css_element']);
164 $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link_exact', '#region-main', 'css_element']);
165 } else {
166 $this->execute('behat_general::should_not_exist_in_the', [$blockname, 'link_exact', $addblock, 'dialogue']);
167 $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'button', $addblock, 'dialogue']);
168 }
392367c4 169 }
fe856690 170}