1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NuvoleWeb\Drupal\DrupalExtension\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Gherkin\Node\TableNode; |
6
|
|
|
use Behat\Mink\Exception\ExpectationException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ChosenFieldContext. |
10
|
|
|
* |
11
|
|
|
* @package NuvoleWeb\Drupal\DrupalExtension\Context |
12
|
|
|
*/ |
13
|
|
|
class ChosenFieldContext extends RawMinkContext { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Fills in chosen form fields with provided table. |
17
|
|
|
* |
18
|
|
|
* @When /^(?:|I )fill in the following chosen fields:$/ |
19
|
|
|
*/ |
20
|
|
|
public function fillChosenFields(TableNode $fields) { |
21
|
|
|
foreach ($fields->getRowsHash() as $field => $value) { |
22
|
|
|
$this->iSetChosenElement($field, $value); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Fills in chosen form fields with provided table. |
28
|
|
|
* |
29
|
|
|
* @When /^(?:|I )unset the following chosen fields:$/ |
30
|
|
|
*/ |
31
|
|
|
public function unsetChosenFields(TableNode $fields) { |
32
|
|
|
foreach ($fields->getRowsHash() as $field => $value) { |
33
|
|
|
$this->iUnSetChosenElement($value, $field); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Select one more option from a Chosen select box. |
39
|
|
|
* |
40
|
|
|
* @When /^I add "([^"]*)" to the chosen element "([^"]*)"$/ |
41
|
|
|
* @When /^I select "([^"]*)" on the Chosen element "([^"]*)"$/ |
42
|
|
|
*/ |
43
|
|
|
public function iAddChosenElement($value, $locator) { |
44
|
|
|
$this->iSetChosenElement($locator, $value); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* This is from a patch which is very much work-in-progress. |
49
|
|
|
* |
50
|
|
|
* @link https://www.drupal.org/node/2562805. |
51
|
|
|
* |
52
|
|
|
* @When /^I set the chosen element "([^"]*)" to "([^"]*)"$/ |
53
|
|
|
*/ |
54
|
|
|
public function iSetChosenElement($locator, $value) { |
55
|
|
|
$session = $this->getSession(); |
56
|
|
|
$el = $session->getPage()->findField($locator); |
57
|
|
|
|
58
|
|
|
if (empty($el)) { |
59
|
|
|
throw new ExpectationException('No such select element ' . $locator, $session); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$element_id = str_replace('-', '_', $el->getAttribute('id')) . '_chosen'; |
63
|
|
|
|
64
|
|
|
$el = $session->getPage()->find('xpath', "//div[@id='{$element_id}']"); |
65
|
|
|
|
66
|
|
|
if ($el->hasClass('chosen-container-single')) { |
67
|
|
|
// This is a single select element. |
68
|
|
|
$el = $session->getPage()->find('xpath', "//div[@id='{$element_id}']/a[@class='chosen-single']"); |
69
|
|
|
$el->click(); |
70
|
|
|
} |
71
|
|
|
elseif ($el->hasClass('chosen-container-multi')) { |
72
|
|
|
// This is a multi select element. |
73
|
|
|
$el = $session->getPage()->find('xpath', "//div[@id='{$element_id}']/ul[@class='chosen-choices']/li[@class='search-field']/input"); |
74
|
|
|
$el->click(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$selector = "//div[@id='{$element_id}']/div[@class='chosen-drop']/ul[@class='chosen-results']/li[text() = '{$value}']"; |
78
|
|
|
$el = $session->getPage()->find('xpath', $selector); |
79
|
|
|
|
80
|
|
|
if (empty($el)) { |
81
|
|
|
throw new ExpectationException('No such option ' . $value . ' in ' . $locator, $session); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$el->click(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Remove an option from a Chosen select box. |
89
|
|
|
* |
90
|
|
|
* @When /^I remove "([^"]*)" from the chosen element "([^"]*)"$/ |
91
|
|
|
* @When I remove :arg1 from the Chosen element :arg2 |
92
|
|
|
*/ |
93
|
|
|
public function iUnSetChosenElement($value, $locator) { |
94
|
|
|
$session = $this->getSession(); |
95
|
|
|
$el = $session->getPage()->findField($locator); |
96
|
|
|
|
97
|
|
|
if (empty($el)) { |
98
|
|
|
throw new ExpectationException('No such select element ' . $locator, $session); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$element_id = str_replace('-', '_', $el->getAttribute('id')) . '_chosen'; |
102
|
|
|
|
103
|
|
|
$el = $session->getPage()->find('xpath', "//div[@id='{$element_id}']"); |
104
|
|
|
if ($el->hasClass('chosen-container-single')) { |
|
|
|
|
105
|
|
|
// This is a single select element, unsetting doesn't make sense. |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$selector = "//div[@id='{$element_id}']/ul[@class='chosen-choices']//li[span = '{$value}']/a"; |
109
|
|
|
$el = $session->getPage()->find('xpath', $selector); |
110
|
|
|
|
111
|
|
|
if (empty($el)) { |
112
|
|
|
throw new ExpectationException('No such option ' . $value . ' selected in ' . $locator, $session); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$el->click(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.