1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NuvoleWeb\Drupal\DrupalExtension\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Behat\Context\SnippetAcceptingContext; |
6
|
|
|
use Behat\Mink\Exception\ExpectationException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class DrupalContext. |
10
|
|
|
* |
11
|
|
|
* @package NuvoleWeb\Drupal\DrupalExtension\Context |
12
|
|
|
*/ |
13
|
|
|
class DrupalContext extends RawDrupalContext implements SnippetAcceptingContext { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Assert viewing content given its type and title. |
17
|
|
|
* |
18
|
|
|
* @param string $type |
19
|
|
|
* Content type machine name. |
20
|
|
|
* @param string $title |
21
|
|
|
* Content title. |
22
|
|
|
* |
23
|
|
|
* @Given I am visiting the :type content :title |
24
|
|
|
* @Given I visit the :type content :title |
25
|
|
|
*/ |
26
|
|
|
public function iAmViewingTheContent($type, $title) { |
27
|
|
|
$this->visitContentPage('view', $type, $title); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Assert editing content given its type and title. |
32
|
|
|
* |
33
|
|
|
* @param string $type |
34
|
|
|
* Content type machine name. |
35
|
|
|
* @param string $title |
36
|
|
|
* Content title. |
37
|
|
|
* |
38
|
|
|
* @Given I am editing the :type content :title |
39
|
|
|
* @Given I edit the :type content :title |
40
|
|
|
*/ |
41
|
|
|
public function iAmEditingTheContent($type, $title) { |
42
|
|
|
$this->visitContentPage('edit', $type, $title); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Assert deleting content given its type and title. |
47
|
|
|
* |
48
|
|
|
* @param string $type |
49
|
|
|
* Content type machine name. |
50
|
|
|
* @param string $title |
51
|
|
|
* Content title. |
52
|
|
|
* |
53
|
|
|
* @Given I am deleting the :type content :title |
54
|
|
|
* @Given I delete the :type content :title |
55
|
|
|
*/ |
56
|
|
|
public function iAmDeletingTheContent($type, $title) { |
57
|
|
|
$this->visitContentPage('delete', $type, $title); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Provides a common step definition callback for node pages. |
62
|
|
|
* |
63
|
|
|
* @param string $op |
64
|
|
|
* The operation being performed: 'view', 'edit', 'delete'. |
65
|
|
|
* @param string $type |
66
|
|
|
* The node type either as id or as label. |
67
|
|
|
* @param string $title |
68
|
|
|
* The node title. |
69
|
|
|
* |
70
|
|
|
* @throws ExpectationException |
71
|
|
|
* When the node does not exist. |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
protected function visitContentPage($op, $type, $title) { |
|
|
|
|
74
|
|
|
$type = $this->getCore()->convertLabelToNodeTypeId($type); |
75
|
|
|
$result = \Drupal::entityQuery('node') |
76
|
|
|
->condition('type', $type) |
77
|
|
|
->condition('title', $title) |
78
|
|
|
->execute(); |
79
|
|
|
|
80
|
|
|
if (!empty($result)) { |
81
|
|
|
$nid = array_shift($result); |
82
|
|
|
$path = [ |
83
|
|
|
'view' => "node/$nid", |
84
|
|
|
'edit' => "node/$nid/edit", |
85
|
|
|
'delete' => "node/$nid/delete", |
86
|
|
|
]; |
87
|
|
|
$this->visitPath($path[$op]); |
88
|
|
|
} |
89
|
|
|
else { |
90
|
|
|
throw new ExpectationException("No node with type '$type' and title '$title' has been found.", $this->getSession()); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
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.