|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NuvoleWeb\Drupal\DrupalExtension\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Behat\Mink\Element\NodeElement; |
|
6
|
|
|
use Behat\Mink\Exception\ExpectationException; |
|
7
|
|
|
use Behat\Mink\Exception\UnsupportedDriverActionException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Trait Generic. |
|
11
|
|
|
* |
|
12
|
|
|
* @package Nuvole\Drupal\Behat\Traits |
|
13
|
|
|
*/ |
|
14
|
|
|
trait Generic { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Checks that the given element is of the given type. |
|
18
|
|
|
* |
|
19
|
|
|
* @param NodeElement $element |
|
20
|
|
|
* The element to check. |
|
21
|
|
|
* @param string $type |
|
22
|
|
|
* The expected type. |
|
23
|
|
|
* |
|
24
|
|
|
* @throws ExpectationException |
|
25
|
|
|
* Thrown when the given element is not of the expected type. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function assertElementType(NodeElement $element, $type) { |
|
28
|
|
|
if ($element->getTagName() !== $type) { |
|
29
|
|
|
throw new ExpectationException("The element is not a '$type'' field.", $this->getSession()); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Assert presence of given field on the page. |
|
35
|
|
|
* |
|
36
|
|
|
* @Then I should see the field :field |
|
37
|
|
|
*/ |
|
38
|
|
View Code Duplication |
public function iShouldSeeTheField($field) { |
|
|
|
|
|
|
39
|
|
|
$element = $this->getSession()->getPage(); |
|
|
|
|
|
|
40
|
|
|
$result = $element->findField($field); |
|
41
|
|
|
try { |
|
42
|
|
|
if ($result && !$result->isVisible()) { |
|
43
|
|
|
throw new \Exception(sprintf("No field '%s' on the page %s", $field, $this->getSession()->getCurrentUrl())); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
catch (UnsupportedDriverActionException $e) { |
|
47
|
|
|
// We catch the UnsupportedDriverActionException exception in case |
|
48
|
|
|
// this step is not being performed by a driver that supports javascript. |
|
49
|
|
|
// All other exceptions are valid. |
|
50
|
|
|
} |
|
51
|
|
|
if (empty($result)) { |
|
52
|
|
|
throw new \Exception(sprintf("No field '%s' on the page %s", $field, $this->getSession()->getCurrentUrl())); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Assert absence of given field. |
|
58
|
|
|
* |
|
59
|
|
|
* @Then I should not see the field :field |
|
60
|
|
|
*/ |
|
61
|
|
View Code Duplication |
public function iShouldNotSeeTheField($field) { |
|
|
|
|
|
|
62
|
|
|
$element = $this->getSession()->getPage(); |
|
|
|
|
|
|
63
|
|
|
$result = $element->findField($field); |
|
64
|
|
|
try { |
|
65
|
|
|
if ($result && $result->isVisible()) { |
|
66
|
|
|
throw new \Exception(sprintf("The field '%s' was present on the page %s and was not supposed to be", $field, $this->getSession()->getCurrentUrl())); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
catch (UnsupportedDriverActionException $e) { |
|
70
|
|
|
// We catch the UnsupportedDriverActionException exception in case |
|
71
|
|
|
// this step is not being performed by a driver that supports javascript. |
|
72
|
|
|
// All other exceptions are valid. |
|
73
|
|
|
if ($result) { |
|
74
|
|
|
throw new \Exception(sprintf("The field '%s' was present on the page %s and was not supposed to be", $field, $this->getSession()->getCurrentUrl())); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Visit taxonomy term page given its type and name. |
|
81
|
|
|
* |
|
82
|
|
|
* @Given I am visiting the :type term :title |
|
83
|
|
|
* @Given I visit the :type term :title |
|
84
|
|
|
*/ |
|
85
|
|
|
public function iAmViewingTheTerm($type, $title) { |
|
86
|
|
|
$this->visitTermPage('view', $type, $title); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Visit taxonomy term edit page given its type and name. |
|
91
|
|
|
* |
|
92
|
|
|
* @Given I am editing the :type term :title |
|
93
|
|
|
* @Given I edit the :type term :title |
|
94
|
|
|
*/ |
|
95
|
|
|
public function iAmEditingTheTerm($type, $title) { |
|
96
|
|
|
$this->visitTermPage('edit', $type, $title); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Provides a common step definition callback for node pages. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $op |
|
103
|
|
|
* The operation being performed: 'view', 'edit', 'delete'. |
|
104
|
|
|
* @param string $type |
|
105
|
|
|
* The node type either as id or as label. |
|
106
|
|
|
* @param string $title |
|
107
|
|
|
* The node title. |
|
108
|
|
|
* |
|
109
|
|
|
* @throws ExpectationException |
|
110
|
|
|
* When the node does not exist. |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function visitTermPage($op, $type, $title) { |
|
113
|
|
|
$type = $this->convertLabelToTermTypeId($type); |
|
|
|
|
|
|
114
|
|
|
$result = \Drupal::entityQuery('taxonomy_term') |
|
115
|
|
|
->condition('vid', $type) |
|
116
|
|
|
->condition('name', $title) |
|
117
|
|
|
->execute(); |
|
118
|
|
|
|
|
119
|
|
|
if (!empty($result)) { |
|
120
|
|
|
$tid = array_shift($result); |
|
121
|
|
|
$path = [ |
|
122
|
|
|
'view' => "taxonomy/term/$tid", |
|
123
|
|
|
'edit' => "taxonomy/term/$tid/edit", |
|
124
|
|
|
'delete' => "taxonomy/term/$tid/delete", |
|
125
|
|
|
]; |
|
126
|
|
|
$this->visitPath($path[$op]); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
else { |
|
129
|
|
|
throw new ExpectationException("No term with vocabulary '$type' and title '$title' has been found.", $this->getSession()); |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Assert first element precedes second one. |
|
135
|
|
|
* |
|
136
|
|
|
* @Then :first should precede :second |
|
137
|
|
|
*/ |
|
138
|
|
|
public function shouldPrecede($first, $second) { |
|
139
|
|
|
$page = $this->getSession()->getPage()->getText(); |
|
|
|
|
|
|
140
|
|
|
$pos1 = strpos($page, $first); |
|
141
|
|
|
if ($pos1 === FALSE) { |
|
142
|
|
|
throw new ExpectationException("Text not found: '$first'.", $this->getSession()); |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
$pos2 = strpos($page, $second); |
|
145
|
|
|
if ($pos2 === FALSE) { |
|
146
|
|
|
throw new ExpectationException("Text not found: '$second'.", $this->getSession()); |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
if ($pos2 < $pos1) { |
|
149
|
|
|
throw new \Exception("Text '$first' does not precede text '$second'."); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.