1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\AssetAdmin\Tests\Behat\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Mink\Element\DocumentElement; |
6
|
|
|
use Behat\Mink\Element\NodeElement; |
7
|
|
|
use SilverStripe\BehatExtension\Context\FixtureContext as BaseFixtureContext; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Context used to create fixtures in the SilverStripe ORM. |
11
|
|
|
*/ |
12
|
|
|
class FixtureContext extends BaseFixtureContext |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Select a gallery item by type and name |
17
|
|
|
* |
18
|
|
|
* @Given /^I (?:(?:click on)|(?:select)) the "([^"]+)" named "([^"]+)" in the gallery$/ |
19
|
|
|
* @param string $type |
20
|
|
|
* @param string $name |
21
|
|
|
*/ |
22
|
|
|
public function stepISelectGalleryItem($type, $name) |
23
|
|
|
{ |
24
|
|
|
$item = $this->getGalleryItem($type, $name); |
25
|
|
|
assertNotNull($item, ucfirst($type) . " named $name could not be found"); |
26
|
|
|
$item->click(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Check the checkbox for a given gallery item |
31
|
|
|
* @Given /^I check the "([^"]+)" named "([^"]+)" in the gallery$/ |
32
|
|
|
* @param string $type |
33
|
|
|
* @param string $name |
34
|
|
|
*/ |
35
|
|
|
public function stepICheckTheGalleryItem($type, $name) |
36
|
|
|
{ |
37
|
|
|
$item = $this->getGalleryItem($type, $name); |
38
|
|
|
assertNotNull($item, ucfirst($type) . " named $name could not be found"); |
39
|
|
|
$checkbox = $item->find('css', 'input[type="checkbox"]'); |
40
|
|
|
assertNotNull($checkbox, "Could not find checkbox for file named {$name}"); |
41
|
|
|
$checkbox->check(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @Then /^I should see the "([^"]+)" named "([^"]+)" in the gallery$/ |
46
|
|
|
* @param $type |
47
|
|
|
* @param $name |
48
|
|
|
*/ |
49
|
|
|
public function iShouldSeeTheGalleryItem($type, $name) |
50
|
|
|
{ |
51
|
|
|
$item = $this->getGalleryItem($type, $name); |
52
|
|
|
assertNotNull($item, ucfirst($type) . " named {$name} could not be found"); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @Then /^I should not see the "([^"]+)" named "([^"]+)" in the gallery$/ |
57
|
|
|
* @param $type |
58
|
|
|
* @param $name |
59
|
|
|
*/ |
60
|
|
|
public function iShouldNotSeeTheGalleryItem($type, $name) |
61
|
|
|
{ |
62
|
|
|
$item = $this->getGalleryItem($type, $name, 0); |
63
|
|
|
assertNull($item, ucfirst($type) . " named {$name} was found when it should not be visible"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @Then /^I should see the "([^"]*)" form$/ |
68
|
|
|
* @param string $id HTML ID of form |
69
|
|
|
*/ |
70
|
|
|
public function iShouldSeeTheForm($id) |
71
|
|
|
{ |
72
|
|
|
/** @var DocumentElement $page */ |
73
|
|
|
$page = $this->getSession()->getPage(); |
74
|
|
|
$form = $this->retry(function () use ($page, $id) { |
75
|
|
|
return $page->find('css', "form#{$id}"); |
76
|
|
|
}); |
77
|
|
|
assertNotNull($form, "form with id $id could not be found"); |
78
|
|
|
assertTrue($form->isVisible(), "form with id $id is not visible"); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @Given /^I click on the latest history item$/ |
83
|
|
|
*/ |
84
|
|
|
public function iClickOnTheLatestHistoryItem() |
85
|
|
|
{ |
86
|
|
|
$this->getSession()->wait( |
87
|
|
|
5000, |
88
|
|
|
"window.jQuery && window.jQuery('.file-history__list li').size() > 0" |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$page = $this->getSession()->getPage(); |
92
|
|
|
|
93
|
|
|
$elements = $page->find('css', '.file-history__list li'); |
94
|
|
|
|
95
|
|
|
if (null === $elements) { |
96
|
|
|
throw new \InvalidArgumentException(sprintf('Could not find list item')); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$elements->click(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @Given /^I attach the file "([^"]*)" to dropzone$/ |
104
|
|
|
* @see MinkContext::attachFileToField() |
105
|
|
|
*/ |
106
|
|
|
public function iAttachTheFileToDropzone($path) |
107
|
|
|
{ |
108
|
|
|
// Get path |
109
|
|
|
$filesPath = $this->getMainContext()->getMinkParameter('files_path'); |
|
|
|
|
110
|
|
|
if ($filesPath) { |
111
|
|
|
$fullPath = rtrim(realpath($filesPath), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$path; |
112
|
|
|
if (is_file($fullPath)) { |
113
|
|
|
$path = $fullPath; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Find field |
118
|
|
|
$input = $this->getSession()->getPage()->find('css', 'input[type="file"].dz-hidden-input'); |
119
|
|
|
assertNotNull($input, "Could not find dz-hidden-input"); |
120
|
|
|
|
121
|
|
|
// Make visible temporarily while attaching |
122
|
|
|
$this->getSession()->executeScript( |
123
|
|
|
<<<EOS |
124
|
|
|
window.jQuery('.dz-hidden-input') |
125
|
|
|
.css('visibility', 'visible') |
126
|
|
|
.width(1) |
127
|
|
|
.height(1); |
128
|
|
|
EOS |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
// Attach via html5 |
132
|
|
|
$input->attachFile($path); |
133
|
|
|
|
134
|
|
|
// Restore hidden state |
135
|
|
|
$this->getSession()->executeScript( |
136
|
|
|
<<<EOS |
137
|
|
|
window.jQuery('.dz-hidden-input') |
138
|
|
|
.css('visibility', 'hidden') |
139
|
|
|
.width(0) |
140
|
|
|
.height(0); |
141
|
|
|
EOS |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Helper for finding items in the visible gallery view |
147
|
|
|
* |
148
|
|
|
* @param string $type type. E.g. 'folder', 'image' |
149
|
|
|
* @param string $name Title of item |
150
|
|
|
* @param int $timeout |
151
|
|
|
* @return NodeElement |
152
|
|
|
*/ |
153
|
|
|
protected function getGalleryItem($type, $name, $timeout = 3) |
154
|
|
|
{ |
155
|
|
|
/** @var DocumentElement $page */ |
156
|
|
|
$page = $this->getSession()->getPage(); |
157
|
|
|
return $this->retry(function () use ($page, $type, $name) { |
158
|
|
|
return $page->find( |
159
|
|
|
'xpath', |
160
|
|
|
"//div[contains(@class, 'gallery-item--{$type}')]//div[contains(text(), '{$name}')]" |
161
|
|
|
); |
162
|
|
|
}, $timeout); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Invoke $try callback for a non-empty result with a given timeout |
167
|
|
|
* |
168
|
|
|
* @param callable $try |
169
|
|
|
* @param int $timeout Number of seconds to retry for |
170
|
|
|
* @return mixed Result of invoking $try, or null if timed out |
171
|
|
|
*/ |
172
|
|
|
protected function retry($try, $timeout = 3) |
173
|
|
|
{ |
174
|
|
|
do { |
175
|
|
|
$result = $try(); |
176
|
|
|
if ($result) { |
177
|
|
|
return $result; |
178
|
|
|
} |
179
|
|
|
sleep(1); |
180
|
|
|
} while (--$timeout >= 0); |
181
|
|
|
return null; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: