Security update for permissions_by_term
[yaffs-website] / vendor / behat / mink-extension / src / Behat / MinkExtension / Context / MinkContext.php
1 <?php
2
3 /*
4  * This file is part of the Behat MinkExtension.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\MinkExtension\Context;
12
13 use Behat\Behat\Context\TranslatableContext;
14 use Behat\Gherkin\Node\TableNode;
15
16 /**
17  * Mink context for Behat BDD tool.
18  * Provides Mink integration and base step definitions.
19  *
20  * @author Konstantin Kudryashov <ever.zet@gmail.com>
21  */
22 class MinkContext extends RawMinkContext implements TranslatableContext
23 {
24     /**
25      * Opens homepage
26      * Example: Given I am on "/"
27      * Example: When I go to "/"
28      * Example: And I go to "/"
29      *
30      * @Given /^(?:|I )am on (?:|the )homepage$/
31      * @When /^(?:|I )go to (?:|the )homepage$/
32      */
33     public function iAmOnHomepage()
34     {
35         $this->visitPath('/');
36     }
37
38     /**
39      * Opens specified page
40      * Example: Given I am on "http://batman.com"
41      * Example: And I am on "/articles/isBatmanBruceWayne"
42      * Example: When I go to "/articles/isBatmanBruceWayne"
43      *
44      * @Given /^(?:|I )am on "(?P<page>[^"]+)"$/
45      * @When /^(?:|I )go to "(?P<page>[^"]+)"$/
46      */
47     public function visit($page)
48     {
49         $this->visitPath($page);
50     }
51
52     /**
53      * Reloads current page
54      * Example: When I reload the page
55      * Example: And I reload the page
56      *
57      * @When /^(?:|I )reload the page$/
58      */
59     public function reload()
60     {
61         $this->getSession()->reload();
62     }
63
64     /**
65      * Moves backward one page in history
66      * Example: When I move backward one page
67      *
68      * @When /^(?:|I )move backward one page$/
69      */
70     public function back()
71     {
72         $this->getSession()->back();
73     }
74
75     /**
76      * Moves forward one page in history
77      * Example: And I move forward one page
78      *
79      * @When /^(?:|I )move forward one page$/
80      */
81     public function forward()
82     {
83         $this->getSession()->forward();
84     }
85
86     /**
87      * Presses button with specified id|name|title|alt|value
88      * Example: When I press "Log In"
89      * Example: And I press "Log In"
90      *
91      * @When /^(?:|I )press "(?P<button>(?:[^"]|\\")*)"$/
92      */
93     public function pressButton($button)
94     {
95         $button = $this->fixStepArgument($button);
96         $this->getSession()->getPage()->pressButton($button);
97     }
98
99     /**
100      * Clicks link with specified id|title|alt|text
101      * Example: When I follow "Log In"
102      * Example: And I follow "Log In"
103      *
104      * @When /^(?:|I )follow "(?P<link>(?:[^"]|\\")*)"$/
105      */
106     public function clickLink($link)
107     {
108         $link = $this->fixStepArgument($link);
109         $this->getSession()->getPage()->clickLink($link);
110     }
111
112     /**
113      * Fills in form field with specified id|name|label|value
114      * Example: When I fill in "username" with: "bwayne"
115      * Example: And I fill in "bwayne" for "username"
116      *
117      * @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/
118      * @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with:$/
119      * @When /^(?:|I )fill in "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/
120      */
121     public function fillField($field, $value)
122     {
123         $field = $this->fixStepArgument($field);
124         $value = $this->fixStepArgument($value);
125         $this->getSession()->getPage()->fillField($field, $value);
126     }
127
128     /**
129      * Fills in form fields with provided table
130      * Example: When I fill in the following"
131      *              | username | bruceWayne |
132      *              | password | iLoveBats123 |
133      * Example: And I fill in the following"
134      *              | username | bruceWayne |
135      *              | password | iLoveBats123 |
136      *
137      * @When /^(?:|I )fill in the following:$/
138      */
139     public function fillFields(TableNode $fields)
140     {
141         foreach ($fields->getRowsHash() as $field => $value) {
142             $this->fillField($field, $value);
143         }
144     }
145
146     /**
147      * Selects option in select field with specified id|name|label|value
148      * Example: When I select "Bats" from "user_fears"
149      * Example: And I select "Bats" from "user_fears"
150      *
151      * @When /^(?:|I )select "(?P<option>(?:[^"]|\\")*)" from "(?P<select>(?:[^"]|\\")*)"$/
152      */
153     public function selectOption($select, $option)
154     {
155         $select = $this->fixStepArgument($select);
156         $option = $this->fixStepArgument($option);
157         $this->getSession()->getPage()->selectFieldOption($select, $option);
158     }
159
160     /**
161      * Selects additional option in select field with specified id|name|label|value
162      * Example: When I additionally select "Deceased" from "parents_alive_status"
163      * Example: And I additionally select "Deceased" from "parents_alive_status"
164      *
165      * @When /^(?:|I )additionally select "(?P<option>(?:[^"]|\\")*)" from "(?P<select>(?:[^"]|\\")*)"$/
166      */
167     public function additionallySelectOption($select, $option)
168     {
169         $select = $this->fixStepArgument($select);
170         $option = $this->fixStepArgument($option);
171         $this->getSession()->getPage()->selectFieldOption($select, $option, true);
172     }
173
174     /**
175      * Checks checkbox with specified id|name|label|value
176      * Example: When I check "Pearl Necklace" from "itemsClaimed"
177      * Example: And I check "Pearl Necklace" from "itemsClaimed"
178      *
179      * @When /^(?:|I )check "(?P<option>(?:[^"]|\\")*)"$/
180      */
181     public function checkOption($option)
182     {
183         $option = $this->fixStepArgument($option);
184         $this->getSession()->getPage()->checkField($option);
185     }
186
187     /**
188      * Unchecks checkbox with specified id|name|label|value
189      * Example: When I uncheck "Broadway Plays" from "hobbies"
190      * Example: And I uncheck "Broadway Plays" from "hobbies"
191      *
192      * @When /^(?:|I )uncheck "(?P<option>(?:[^"]|\\")*)"$/
193      */
194     public function uncheckOption($option)
195     {
196         $option = $this->fixStepArgument($option);
197         $this->getSession()->getPage()->uncheckField($option);
198     }
199
200     /**
201      * Attaches file to field with specified id|name|label|value
202      * Example: When I attach "bwayne_profile.png" to "profileImageUpload"
203      * Example: And I attach "bwayne_profile.png" to "profileImageUpload"
204      *
205      * @When /^(?:|I )attach the file "(?P<path>[^"]*)" to "(?P<field>(?:[^"]|\\")*)"$/
206      */
207     public function attachFileToField($field, $path)
208     {
209         $field = $this->fixStepArgument($field);
210
211         if ($this->getMinkParameter('files_path')) {
212             $fullPath = rtrim(realpath($this->getMinkParameter('files_path')), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$path;
213             if (is_file($fullPath)) {
214                 $path = $fullPath;
215             }
216         }
217
218         $this->getSession()->getPage()->attachFileToField($field, $path);
219     }
220
221     /**
222      * Checks, that current page PATH is equal to specified
223      * Example: Then I should be on "/"
224      * Example: And I should be on "/bats"
225      * Example: And I should be on "http://google.com"
226      *
227      * @Then /^(?:|I )should be on "(?P<page>[^"]+)"$/
228      */
229     public function assertPageAddress($page)
230     {
231         $this->assertSession()->addressEquals($this->locatePath($page));
232     }
233
234     /**
235      * Checks, that current page is the homepage
236      * Example: Then I should be on the homepage
237      * Example: And I should be on the homepage
238      *
239      * @Then /^(?:|I )should be on (?:|the )homepage$/
240      */
241     public function assertHomepage()
242     {
243         $this->assertSession()->addressEquals($this->locatePath('/'));
244     }
245
246     /**
247      * Checks, that current page PATH matches regular expression
248      * Example: Then the url should match "superman is dead"
249      * Example: Then the uri should match "log in"
250      * Example: And the url should match "log in"
251      *
252      * @Then /^the (?i)url(?-i) should match (?P<pattern>"(?:[^"]|\\")*")$/
253      */
254     public function assertUrlRegExp($pattern)
255     {
256         $this->assertSession()->addressMatches($this->fixStepArgument($pattern));
257     }
258
259     /**
260      * Checks, that current page response status is equal to specified
261      * Example: Then the response status code should be 200
262      * Example: And the response status code should be 400
263      *
264      * @Then /^the response status code should be (?P<code>\d+)$/
265      */
266     public function assertResponseStatus($code)
267     {
268         $this->assertSession()->statusCodeEquals($code);
269     }
270
271     /**
272      * Checks, that current page response status is not equal to specified
273      * Example: Then the response status code should not be 501
274      * Example: And the response status code should not be 404
275      *
276      * @Then /^the response status code should not be (?P<code>\d+)$/
277      */
278     public function assertResponseStatusIsNot($code)
279     {
280         $this->assertSession()->statusCodeNotEquals($code);
281     }
282
283     /**
284      * Checks, that page contains specified text
285      * Example: Then I should see "Who is the Batman?"
286      * Example: And I should see "Who is the Batman?"
287      *
288      * @Then /^(?:|I )should see "(?P<text>(?:[^"]|\\")*)"$/
289      */
290     public function assertPageContainsText($text)
291     {
292         $this->assertSession()->pageTextContains($this->fixStepArgument($text));
293     }
294
295     /**
296      * Checks, that page doesn't contain specified text
297      * Example: Then I should not see "Batman is Bruce Wayne"
298      * Example: And I should not see "Batman is Bruce Wayne"
299      *
300      * @Then /^(?:|I )should not see "(?P<text>(?:[^"]|\\")*)"$/
301      */
302     public function assertPageNotContainsText($text)
303     {
304         $this->assertSession()->pageTextNotContains($this->fixStepArgument($text));
305     }
306
307     /**
308      * Checks, that page contains text matching specified pattern
309      * Example: Then I should see text matching "Batman, the vigilante"
310      * Example: And I should not see "Batman, the vigilante"
311      *
312      * @Then /^(?:|I )should see text matching (?P<pattern>"(?:[^"]|\\")*")$/
313      */
314     public function assertPageMatchesText($pattern)
315     {
316         $this->assertSession()->pageTextMatches($this->fixStepArgument($pattern));
317     }
318
319     /**
320      * Checks, that page doesn't contain text matching specified pattern
321      * Example: Then I should see text matching "Bruce Wayne, the vigilante"
322      * Example: And I should not see "Bruce Wayne, the vigilante"
323      *
324      * @Then /^(?:|I )should not see text matching (?P<pattern>"(?:[^"]|\\")*")$/
325      */
326     public function assertPageNotMatchesText($pattern)
327     {
328         $this->assertSession()->pageTextNotMatches($this->fixStepArgument($pattern));
329     }
330
331     /**
332      * Checks, that HTML response contains specified string
333      * Example: Then the response should contain "Batman is the hero Gotham deserves."
334      * Example: And the response should contain "Batman is the hero Gotham deserves."
335      *
336      * @Then /^the response should contain "(?P<text>(?:[^"]|\\")*)"$/
337      */
338     public function assertResponseContains($text)
339     {
340         $this->assertSession()->responseContains($this->fixStepArgument($text));
341     }
342
343     /**
344      * Checks, that HTML response doesn't contain specified string
345      * Example: Then the response should not contain "Bruce Wayne is a billionaire, play-boy, vigilante."
346      * Example: And the response should not contain "Bruce Wayne is a billionaire, play-boy, vigilante."
347      *
348      * @Then /^the response should not contain "(?P<text>(?:[^"]|\\")*)"$/
349      */
350     public function assertResponseNotContains($text)
351     {
352         $this->assertSession()->responseNotContains($this->fixStepArgument($text));
353     }
354
355     /**
356      * Checks, that element with specified CSS contains specified text
357      * Example: Then I should see "Batman" in the "heroes_list" element
358      * Example: And I should see "Batman" in the "heroes_list" element
359      *
360      * @Then /^(?:|I )should see "(?P<text>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" element$/
361      */
362     public function assertElementContainsText($element, $text)
363     {
364         $this->assertSession()->elementTextContains('css', $element, $this->fixStepArgument($text));
365     }
366
367     /**
368      * Checks, that element with specified CSS doesn't contain specified text
369      * Example: Then I should not see "Bruce Wayne" in the "heroes_alter_egos" element
370      * Example: And I should not see "Bruce Wayne" in the "heroes_alter_egos" element
371      *
372      * @Then /^(?:|I )should not see "(?P<text>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" element$/
373      */
374     public function assertElementNotContainsText($element, $text)
375     {
376         $this->assertSession()->elementTextNotContains('css', $element, $this->fixStepArgument($text));
377     }
378
379     /**
380      * Checks, that element with specified CSS contains specified HTML
381      * Example: Then the "body" element should contain "style=\"color:black;\""
382      * Example: And the "body" element should contain "style=\"color:black;\""
383      *
384      * @Then /^the "(?P<element>[^"]*)" element should contain "(?P<value>(?:[^"]|\\")*)"$/
385      */
386     public function assertElementContains($element, $value)
387     {
388         $this->assertSession()->elementContains('css', $element, $this->fixStepArgument($value));
389     }
390
391     /**
392      * Checks, that element with specified CSS doesn't contain specified HTML
393      * Example: Then the "body" element should not contain "style=\"color:black;\""
394      * Example: And the "body" element should not contain "style=\"color:black;\""
395      *
396      * @Then /^the "(?P<element>[^"]*)" element should not contain "(?P<value>(?:[^"]|\\")*)"$/
397      */
398     public function assertElementNotContains($element, $value)
399     {
400         $this->assertSession()->elementNotContains('css', $element, $this->fixStepArgument($value));
401     }
402
403     /**
404      * Checks, that element with specified CSS exists on page
405      * Example: Then I should see a "body" element
406      * Example: And I should see a "body" element
407      *
408      * @Then /^(?:|I )should see an? "(?P<element>[^"]*)" element$/
409      */
410     public function assertElementOnPage($element)
411     {
412         $this->assertSession()->elementExists('css', $element);
413     }
414
415     /**
416      * Checks, that element with specified CSS doesn't exist on page
417      * Example: Then I should not see a "canvas" element
418      * Example: And I should not see a "canvas" element
419      *
420      * @Then /^(?:|I )should not see an? "(?P<element>[^"]*)" element$/
421      */
422     public function assertElementNotOnPage($element)
423     {
424         $this->assertSession()->elementNotExists('css', $element);
425     }
426
427     /**
428      * Checks, that form field with specified id|name|label|value has specified value
429      * Example: Then the "username" field should contain "bwayne"
430      * Example: And the "username" field should contain "bwayne"
431      *
432      * @Then /^the "(?P<field>(?:[^"]|\\")*)" field should contain "(?P<value>(?:[^"]|\\")*)"$/
433      */
434     public function assertFieldContains($field, $value)
435     {
436         $field = $this->fixStepArgument($field);
437         $value = $this->fixStepArgument($value);
438         $this->assertSession()->fieldValueEquals($field, $value);
439     }
440
441     /**
442      * Checks, that form field with specified id|name|label|value doesn't have specified value
443      * Example: Then the "username" field should not contain "batman"
444      * Example: And the "username" field should not contain "batman"
445      *
446      * @Then /^the "(?P<field>(?:[^"]|\\")*)" field should not contain "(?P<value>(?:[^"]|\\")*)"$/
447      */
448     public function assertFieldNotContains($field, $value)
449     {
450         $field = $this->fixStepArgument($field);
451         $value = $this->fixStepArgument($value);
452         $this->assertSession()->fieldValueNotEquals($field, $value);
453     }
454
455     /**
456      * Checks, that (?P<num>\d+) CSS elements exist on the page
457      * Example: Then I should see 5 "div" elements
458      * Example: And I should see 5 "div" elements
459      *
460      * @Then /^(?:|I )should see (?P<num>\d+) "(?P<element>[^"]*)" elements?$/
461      */
462     public function assertNumElements($num, $element)
463     {
464         $this->assertSession()->elementsCount('css', $element, intval($num));
465     }
466
467     /**
468      * Checks, that checkbox with specified in|name|label|value is checked
469      * Example: Then the "remember_me" checkbox should be checked
470      * Example: And the "remember_me" checkbox is checked
471      *
472      * @Then /^the "(?P<checkbox>(?:[^"]|\\")*)" checkbox should be checked$/
473      * @Then /^the checkbox "(?P<checkbox>(?:[^"]|\\")*)" (?:is|should be) checked$/
474      */
475     public function assertCheckboxChecked($checkbox)
476     {
477         $this->assertSession()->checkboxChecked($this->fixStepArgument($checkbox));
478     }
479
480     /**
481      * Checks, that checkbox with specified in|name|label|value is unchecked
482      * Example: Then the "newsletter" checkbox should be unchecked
483      * Example: Then the "newsletter" checkbox should not be checked
484      * Example: And the "newsletter" checkbox is unchecked
485      *
486      * @Then /^the "(?P<checkbox>(?:[^"]|\\")*)" checkbox should not be checked$/
487      * @Then /^the checkbox "(?P<checkbox>(?:[^"]|\\")*)" should (?:be unchecked|not be checked)$/
488      * @Then /^the checkbox "(?P<checkbox>(?:[^"]|\\")*)" is (?:unchecked|not checked)$/
489      */
490     public function assertCheckboxNotChecked($checkbox)
491     {
492         $this->assertSession()->checkboxNotChecked($this->fixStepArgument($checkbox));
493     }
494
495     /**
496      * Prints current URL to console.
497      * Example: Then print current URL
498      * Example: And print current URL
499      *
500      * @Then /^print current URL$/
501      */
502     public function printCurrentUrl()
503     {
504         echo $this->getSession()->getCurrentUrl();
505     }
506
507     /**
508      * Prints last response to console
509      * Example: Then print current response
510      * Example: And print current response
511      *
512      * @Then /^print last response$/
513      */
514     public function printLastResponse()
515     {
516         echo (
517             $this->getSession()->getCurrentUrl()."\n\n".
518             $this->getSession()->getPage()->getContent()
519         );
520     }
521
522     /**
523      * Opens last response content in browser
524      * Example: Then show last response
525      * Example: And show last response
526      *
527      * @Then /^show last response$/
528      */
529     public function showLastResponse()
530     {
531         if (null === $this->getMinkParameter('show_cmd')) {
532             throw new \RuntimeException('Set "show_cmd" parameter in behat.yml to be able to open page in browser (ex.: "show_cmd: firefox %s")');
533         }
534
535         $filename = rtrim($this->getMinkParameter('show_tmp_dir'), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.uniqid().'.html';
536         file_put_contents($filename, $this->getSession()->getPage()->getContent());
537         system(sprintf($this->getMinkParameter('show_cmd'), escapeshellarg($filename)));
538     }
539
540     /**
541      * Returns list of definition translation resources paths
542      *
543      * @return array
544      */
545     public static function getTranslationResources()
546     {
547         return self::getMinkTranslationResources();
548     }
549
550     /**
551      * Returns list of definition translation resources paths for this dictionary
552      *
553      * @return array
554      */
555     public static function getMinkTranslationResources()
556     {
557         return glob(__DIR__.'/../../../../i18n/*.xliff');
558     }
559
560     /**
561      * Returns fixed step argument (with \\" replaced back to ")
562      *
563      * @param string $argument
564      *
565      * @return string
566      */
567     protected function fixStepArgument($argument)
568     {
569         return str_replace('\\"', '"', $argument);
570     }
571 }