These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Drupal\DrupalExtension\Context; |
||
4 | |||
5 | use Behat\MinkExtension\Context\RawMinkContext; |
||
6 | |||
7 | /** |
||
8 | * Extensions to the Mink Extension. |
||
9 | */ |
||
10 | class MarkupContext extends RawMinkContext |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * Return a region from the current page. |
||
15 | * |
||
16 | * @throws \Exception |
||
17 | * If region cannot be found. |
||
18 | * |
||
19 | * @param string $region |
||
20 | * The machine name of the region to return. |
||
21 | * |
||
22 | * @return \Behat\Mink\Element\NodeElement |
||
23 | * |
||
24 | * @todo this should be a trait when PHP 5.3 support is dropped. |
||
25 | */ |
||
26 | View Code Duplication | public function getRegion($region) |
|
0 ignored issues
–
show
|
|||
27 | { |
||
28 | $session = $this->getSession(); |
||
29 | $regionObj = $session->getPage()->find('region', $region); |
||
30 | if (!$regionObj) { |
||
31 | throw new \Exception(sprintf('No region "%s" found on the page %s.', $region, $session->getCurrentUrl())); |
||
32 | } |
||
33 | |||
34 | return $regionObj; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Checks if a button with id|name|title|alt|value exists in a region |
||
39 | * |
||
40 | * @Then I should see the button :button in the :region( region) |
||
41 | * @Then I should see the :button button in the :region( region) |
||
42 | * |
||
43 | * @param $button |
||
44 | * string The id|name|title|alt|value of the button |
||
45 | * @param $region |
||
46 | * string The region in which the button should be found |
||
47 | * |
||
48 | * @throws \Exception |
||
49 | * If region or button within it cannot be found. |
||
50 | */ |
||
51 | View Code Duplication | public function assertRegionButton($button, $region) |
|
52 | { |
||
53 | $regionObj = $this->getRegion($region); |
||
54 | |||
55 | $buttonObj = $regionObj->findButton($button); |
||
56 | if (empty($buttonObj)) { |
||
57 | throw new \Exception(sprintf("The button '%s' was not found in the region '%s' on the page %s", $button, $region, $this->getSession()->getCurrentUrl())); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @Then I( should) see the :tag element in the :region( region) |
||
63 | */ |
||
64 | View Code Duplication | public function assertRegionElement($tag, $region) |
|
65 | { |
||
66 | $regionObj = $this->getRegion($region); |
||
67 | $elements = $regionObj->findAll('css', $tag); |
||
68 | if (!empty($elements)) { |
||
69 | return; |
||
70 | } |
||
71 | throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl())); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @Then I( should) not see the :tag element in the :region( region) |
||
76 | */ |
||
77 | View Code Duplication | public function assertNotRegionElement($tag, $region) |
|
78 | { |
||
79 | $regionObj = $this->getRegion($region); |
||
80 | $result = $regionObj->findAll('css', $tag); |
||
81 | if (!empty($result)) { |
||
82 | throw new \Exception(sprintf('The element "%s" was found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl())); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @Then I( should) not see :text in the :tag element in the :region( region) |
||
88 | */ |
||
89 | public function assertNotRegionElementText($text, $tag, $region) |
||
90 | { |
||
91 | $regionObj = $this->getRegion($region); |
||
92 | $results = $regionObj->findAll('css', $tag); |
||
93 | if (!empty($results)) { |
||
94 | foreach ($results as $result) { |
||
95 | if ($result->getText() == $text) { |
||
96 | throw new \Exception(sprintf('The text "%s" was found in the "%s" element in the "%s" region on the page %s', $text, $tag, $region, $this->getSession()->getCurrentUrl())); |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @Then I( should) see the :tag element with the :attribute attribute set to :value in the :region( region) |
||
104 | */ |
||
105 | public function assertRegionElementAttribute($tag, $attribute, $value, $region) |
||
106 | { |
||
107 | $regionObj = $this->getRegion($region); |
||
108 | $elements = $regionObj->findAll('css', $tag); |
||
109 | if (empty($elements)) { |
||
110 | throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl())); |
||
111 | } |
||
112 | if (!empty($attribute)) { |
||
113 | $found = false; |
||
114 | $attrfound = false; |
||
115 | foreach ($elements as $element) { |
||
116 | $attr = $element->getAttribute($attribute); |
||
117 | if (!empty($attr)) { |
||
118 | $attrfound = true; |
||
119 | if (strpos($attr, "$value") !== false) { |
||
120 | $found = true; |
||
121 | break; |
||
122 | } |
||
123 | } |
||
124 | } |
||
125 | if (!$found) { |
||
126 | if (!$attrfound) { |
||
127 | throw new \Exception(sprintf('The "%s" attribute is not present on the element "%s" in the "%s" region on the page %s', $attribute, $tag, $region, $this->getSession()->getCurrentUrl())); |
||
128 | } else { |
||
129 | throw new \Exception(sprintf('The "%s" attribute does not equal "%s" on the element "%s" in the "%s" region on the page %s', $attribute, $value, $tag, $region, $this->getSession()->getCurrentUrl())); |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @Then I( should) see :text in the :tag element with the :attribute attribute set to :value in the :region( region) |
||
137 | */ |
||
138 | public function assertRegionElementTextAttribute($text, $tag, $attribute, $value, $region) |
||
139 | { |
||
140 | $regionObj = $this->getRegion($region); |
||
141 | $elements = $regionObj->findAll('css', $tag); |
||
142 | if (empty($elements)) { |
||
143 | throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl())); |
||
144 | } |
||
145 | |||
146 | $found = false; |
||
147 | foreach ($elements as $element) { |
||
148 | if ($element->getText() == $text) { |
||
149 | $found = true; |
||
150 | break; |
||
151 | } |
||
152 | } |
||
153 | if (!$found) { |
||
154 | throw new \Exception(sprintf('The text "%s" was not found in the "%s" element in the "%s" region on the page %s', $text, $tag, $region, $this->getSession()->getCurrentUrl())); |
||
155 | } |
||
156 | |||
157 | if (!empty($attribute)) { |
||
158 | $attr = $element->getAttribute($attribute); |
||
159 | if (empty($attr)) { |
||
160 | throw new \Exception(sprintf('The "%s" attribute is not present on the element "%s" in the "%s" region on the page %s', $attribute, $tag, $region, $this->getSession()->getCurrentUrl())); |
||
161 | } |
||
162 | View Code Duplication | if (strpos($attr, "$value") === false) { |
|
163 | throw new \Exception(sprintf('The "%s" attribute does not equal "%s" on the element "%s" in the "%s" region on the page %s', $attribute, $value, $tag, $region, $this->getSession()->getCurrentUrl())); |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @Then I( should) see :text in the :tag element with the :property CSS property set to :value in the :region( region) |
||
170 | */ |
||
171 | public function assertRegionElementTextCss($text, $tag, $property, $value, $region) |
||
172 | { |
||
173 | $regionObj = $this->getRegion($region); |
||
174 | $elements = $regionObj->findAll('css', $tag); |
||
175 | if (empty($elements)) { |
||
176 | throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl())); |
||
177 | } |
||
178 | |||
179 | $found = false; |
||
180 | foreach ($elements as $element) { |
||
181 | if ($element->getText() == $text) { |
||
182 | $found = true; |
||
183 | break; |
||
184 | } |
||
185 | } |
||
186 | if (!$found) { |
||
187 | throw new \Exception(sprintf('The text "%s" was not found in the "%s" element in the "%s" region on the page %s', $text, $tag, $region, $this->getSession()->getCurrentUrl())); |
||
188 | } |
||
189 | |||
190 | $found = false; |
||
191 | if (!empty($property)) { |
||
192 | $style = $element->getAttribute('style'); |
||
193 | $rules = explode(";", $style); |
||
194 | foreach ($rules as $rule) { |
||
195 | if (strpos($rule, $property) !== false) { |
||
196 | View Code Duplication | if (strpos($rule, $value) === false) { |
|
197 | throw new \Exception(sprintf('The "%s" style property does not equal "%s" on the element "%s" in the "%s" region on the page %s', $property, $value, $tag, $region, $this->getSession()->getCurrentUrl())); |
||
198 | } |
||
199 | $found = true; |
||
200 | break; |
||
201 | } |
||
202 | } |
||
203 | if (!$found) { |
||
204 | throw new \Exception(sprintf('The "%s" style property was not found in the "%s" element in the "%s" region on the page %s', $property, $tag, $region, $this->getSession()->getCurrentUrl())); |
||
205 | } |
||
206 | } |
||
207 | } |
||
208 | } |
||
209 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.