1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NuvoleWeb\Drupal\DrupalExtension\Traits; |
4
|
|
|
|
5
|
|
|
use Behat\Mink\Exception\ExpectationException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait Generic. |
9
|
|
|
* |
10
|
|
|
* @package Nuvole\Drupal\Behat\Traits |
11
|
|
|
*/ |
12
|
|
|
trait Generic { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Visit taxonomy term page given its type and name. |
16
|
|
|
* |
17
|
|
|
* @Given I am visiting the :type term :title |
18
|
|
|
* @Given I visit the :type term :title |
19
|
|
|
*/ |
20
|
|
|
public function iAmViewingTheTerm($type, $title) { |
21
|
|
|
$this->visitTermPage('view', $type, $title); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Visit taxonomy term edit page given its type and name. |
26
|
|
|
* |
27
|
|
|
* @Given I am editing the :type term :title |
28
|
|
|
* @Given I edit the :type term :title |
29
|
|
|
*/ |
30
|
|
|
public function iAmEditingTheTerm($type, $title) { |
31
|
|
|
$this->visitTermPage('edit', $type, $title); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Provides a common step definition callback for node pages. |
36
|
|
|
* |
37
|
|
|
* @param string $op |
38
|
|
|
* The operation being performed: 'view', 'edit', 'delete'. |
39
|
|
|
* @param string $type |
40
|
|
|
* The node type either as id or as label. |
41
|
|
|
* @param string $title |
42
|
|
|
* The node title. |
43
|
|
|
* |
44
|
|
|
* @throws ExpectationException |
45
|
|
|
* When the node does not exist. |
46
|
|
|
*/ |
47
|
|
|
protected function visitTermPage($op, $type, $title) { |
48
|
|
|
$type = $this->convertLabelToTermTypeId($type); |
|
|
|
|
49
|
|
|
$result = \Drupal::entityQuery('taxonomy_term') |
50
|
|
|
->condition('vid', $type) |
51
|
|
|
->condition('name', $title) |
52
|
|
|
->execute(); |
53
|
|
|
|
54
|
|
|
if (!empty($result)) { |
55
|
|
|
$tid = array_shift($result); |
56
|
|
|
$path = [ |
57
|
|
|
'view' => "taxonomy/term/$tid", |
58
|
|
|
'edit' => "taxonomy/term/$tid/edit", |
59
|
|
|
'delete' => "taxonomy/term/$tid/delete", |
60
|
|
|
]; |
61
|
|
|
$this->visitPath($path[$op]); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
else { |
64
|
|
|
throw new ExpectationException("No term with vocabulary '$type' and title '$title' has been found.", $this->getSession()); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
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
Idable
provides a methodequalsId
that 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.