Security update for permissions_by_term
[yaffs-website] / vendor / drupal / drupal-extension / src / Drupal / DrupalExtension / Context / MarkupContext.php
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    * Return a region from the current page.
14    *
15    * @throws \Exception
16    *   If region cannot be found.
17    *
18    * @param string $region
19    *   The machine name of the region to return.
20    *
21    * @return \Behat\Mink\Element\NodeElement
22    *
23    * @todo this should be a trait when PHP 5.3 support is dropped.
24    */
25   public function getRegion($region) {
26     $session = $this->getSession();
27     $regionObj = $session->getPage()->find('region', $region);
28     if (!$regionObj) {
29       throw new \Exception(sprintf('No region "%s" found on the page %s.', $region, $session->getCurrentUrl()));
30     }
31
32     return $regionObj;
33   }
34
35   /**
36    * Checks if a button with id|name|title|alt|value exists in a region
37    *
38    * @Then I should see the button :button in the :region( region)
39    * @Then I should see the :button button in the :region( region)
40    *
41    * @param $button
42    *   string The id|name|title|alt|value of the button
43    * @param $region
44    *   string The region in which the button should be found
45    *
46    * @throws \Exception
47    *   If region or button within it cannot be found.
48    */
49   public function assertRegionButton($button, $region) {
50     $regionObj = $this->getRegion($region);
51
52     $buttonObj = $regionObj->findButton($button);
53     if (empty($buttonObj)) {
54       throw new \Exception(sprintf("The button '%s' was not found in the region '%s' on the page %s", $button, $region, $this->getSession()->getCurrentUrl()));
55     }
56   }
57
58   /**
59    * @Then I( should) see the :tag element in the :region( region)
60    */
61   public function assertRegionElement($tag, $region) {
62     $regionObj = $this->getRegion($region);
63     $elements = $regionObj->findAll('css', $tag);
64     if (!empty($elements)) {
65       return;
66     }
67     throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
68   }
69
70   /**
71    * @Then I( should) not see the :tag element in the :region( region)
72    */
73   public function assertNotRegionElement($tag, $region) {
74     $regionObj = $this->getRegion($region);
75     $result = $regionObj->findAll('css', $tag);
76     if (!empty($result)) {
77       throw new \Exception(sprintf('The element "%s" was found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
78     }
79   }
80
81   /**
82    * @Then I( should) not see :text in the :tag element in the :region( region)
83    */
84   public function assertNotRegionElementText($text, $tag, $region) {
85     $regionObj = $this->getRegion($region);
86     $results = $regionObj->findAll('css', $tag);
87     if (!empty($results)) {
88       foreach ($results as $result) {
89         if ($result->getText() == $text) {
90           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()));
91         }
92       }
93     }
94   }
95
96   /**
97    * @Then I( should) see the :tag element with the :attribute attribute set to :value in the :region( region)
98    */
99   public function assertRegionElementAttribute($tag, $attribute, $value, $region) {
100     $regionObj = $this->getRegion($region);
101     $elements = $regionObj->findAll('css', $tag);
102     if (empty($elements)) {
103       throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
104     }
105     if (!empty($attribute)) {
106       $found = FALSE;
107       foreach ($elements as $element) {
108         $attr = $element->getAttribute($attribute);
109         if (!empty($attr)) {
110           $found = TRUE;
111           if (strpos($attr, "$value") === FALSE) {
112             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()));
113           }
114           break;
115         }
116       }
117       if (!$found) {
118         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()));
119       }
120     }
121   }
122
123   /**
124    * @Then I( should) see :text in the :tag element with the :attribute attribute set to :value in the :region( region)
125    */
126   public function assertRegionElementTextAttribute($text, $tag, $attribute, $value, $region) {
127     $regionObj = $this->getRegion($region);
128     $elements = $regionObj->findAll('css', $tag);
129     if (empty($elements)) {
130       throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
131     }
132
133     $found = FALSE;
134     foreach ($elements as $element) {
135       if ($element->getText() == $text) {
136         $found = TRUE;
137         break;
138       }
139     }
140     if (!$found) {
141       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()));
142     }
143
144     if (!empty($attribute)) {
145       $attr = $element->getAttribute($attribute);
146       if (empty($attr)) {
147         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()));
148       }
149       if (strpos($attr, "$value") === FALSE) {
150         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()));
151       }
152     }
153   }
154
155   /**
156    * @Then I( should) see :text in the :tag element with the :property CSS property set to :value in the :region( region)
157    */
158   public function assertRegionElementTextCss($text, $tag, $property, $value, $region) {
159     $regionObj = $this->getRegion($region);
160     $elements = $regionObj->findAll('css', $tag);
161     if (empty($elements)) {
162       throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
163     }
164
165     $found = FALSE;
166     foreach ($elements as $element) {
167       if ($element->getText() == $text) {
168         $found = TRUE;
169         break;
170       }
171     }
172     if (!$found) {
173       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()));
174     }
175
176     $found = FALSE;
177     if (!empty($property)) {
178       $style = $element->getAttribute('style');
179       $rules = explode(";", $style);
180       foreach ($rules as $rule) {
181         if (strpos($rule, $property) !== FALSE) {
182           if (strpos($rule, $value) === FALSE) {
183             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()));
184           }
185           $found = TRUE;
186           break;
187         }
188       }
189       if (!$found) {
190         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()));
191       }
192     }
193   }
194 }