Completed
Push — master ( ca5016...ff385a )
by Jonathan
12s
created

Drupal/DrupalExtension/Context/MarkupContext.php (1 issue)

Upgrade to new PHP Analysis Engine

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
   * 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 View Code Duplication
  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 View Code Duplication
  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 View Code Duplication
  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
      $attrfound = FALSE;
108
      foreach ($elements as $element) {
109
        $attr = $element->getAttribute($attribute);
110
        if (!empty($attr)) {
111
          $attrfound = TRUE;
112
          if (strpos($attr, "$value") !== FALSE) {
113
            $found = TRUE;
114
            break;
115
          }
116
        }
117
      }
118
      if (!$found) {
119
        if (!$attrfound) {
120
          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()));
121
        }
122
        else {
123
          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()));
124
        }
125
      }
126
    }
127
  }
128
129
  /**
130
   * @Then I( should) see :text in the :tag element with the :attribute attribute set to :value in the :region( region)
131
   */
132
  public function assertRegionElementTextAttribute($text, $tag, $attribute, $value, $region) {
133
    $regionObj = $this->getRegion($region);
134
    $elements = $regionObj->findAll('css', $tag);
135
    if (empty($elements)) {
136
      throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
137
    }
138
139
    $found = FALSE;
140
    foreach ($elements as $element) {
141
      if ($element->getText() == $text) {
142
        $found = TRUE;
143
        break;
144
      }
145
    }
146
    if (!$found) {
147
      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()));
148
    }
149
150
    if (!empty($attribute)) {
151
      $attr = $element->getAttribute($attribute);
152
      if (empty($attr)) {
153
        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()));
154
      }
155 View Code Duplication
      if (strpos($attr, "$value") === FALSE) {
156
        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()));
157
      }
158
    }
159
  }
160
161
  /**
162
   * @Then I( should) see :text in the :tag element with the :property CSS property set to :value in the :region( region)
163
   */
164
  public function assertRegionElementTextCss($text, $tag, $property, $value, $region) {
165
    $regionObj = $this->getRegion($region);
166
    $elements = $regionObj->findAll('css', $tag);
167
    if (empty($elements)) {
168
      throw new \Exception(sprintf('The element "%s" was not found in the "%s" region on the page %s', $tag, $region, $this->getSession()->getCurrentUrl()));
169
    }
170
171
    $found = FALSE;
172
    foreach ($elements as $element) {
173
      if ($element->getText() == $text) {
174
        $found = TRUE;
175
        break;
176
      }
177
    }
178
    if (!$found) {
179
      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()));
180
    }
181
182
    $found = FALSE;
183
    if (!empty($property)) {
184
      $style = $element->getAttribute('style');
185
      $rules = explode(";", $style);
186
      foreach ($rules as $rule) {
187
        if (strpos($rule, $property) !== FALSE) {
188 View Code Duplication
          if (strpos($rule, $value) === FALSE) {
0 ignored issues
show
This code seems to be duplicated across your project.

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.

Loading history...
189
            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()));
190
          }
191
          $found = TRUE;
192
          break;
193
        }
194
      }
195
      if (!$found) {
196
        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()));
197
      }
198
    }
199
  }
200
}
201