FixtureContext::aPageContaining()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 2
dl 0
loc 19
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Tests\Behat\Context;
4
5
use Behat\Mink\Element\DocumentElement;
0 ignored issues
show
Bug introduced by
The type Behat\Mink\Element\DocumentElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Behat\Mink\Element\NodeElement;
0 ignored issues
show
Bug introduced by
The type Behat\Mink\Element\NodeElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Page;
0 ignored issues
show
Bug introduced by
The type Page was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use SilverStripe\Assets\Image;
9
use SilverStripe\BehatExtension\Context\FixtureContext as BaseFixtureContext;
0 ignored issues
show
Bug introduced by
The type SilverStripe\BehatExtension\Context\FixtureContext was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SilverStripe\BehatExtension\Utility\StepHelper;
0 ignored issues
show
Bug introduced by
The type SilverStripe\BehatExtension\Utility\StepHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
/**
13
 * Context used to create fixtures in the SilverStripe ORM.
14
 */
15
class FixtureContext extends BaseFixtureContext
16
{
17
    use StepHelper;
18
19
    /**
20
     * Select a gallery item by type and name
21
     *
22
     * @Given /^I (?:(?:click on)|(?:select)) the (?:file|folder) named "([^"]+)" in the gallery$/
23
     * @param string $name
24
     */
25
    public function stepISelectGalleryItem($name)
26
    {
27
        $item = $this->getGalleryItem($name);
28
        assertNotNull($item, "File named $name could not be found");
0 ignored issues
show
Bug introduced by
The function assertNotNull was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        /** @scrutinizer ignore-call */ 
29
        assertNotNull($item, "File named $name could not be found");
Loading history...
29
        $item->click();
30
    }
31
32
    /**
33
     * Check the checkbox for a given gallery item
34
     * @Given /^I check the (?:file|folder) named "([^"]+)" in the gallery$/
35
     * @param string $name
36
     */
37
    public function stepICheckTheGalleryItem($name)
38
    {
39
        $item = $this->getGalleryItem($name);
40
        assertNotNull($item, "File named $name could not be found");
0 ignored issues
show
Bug introduced by
The function assertNotNull was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        /** @scrutinizer ignore-call */ 
41
        assertNotNull($item, "File named $name could not be found");
Loading history...
41
        $checkboxLabel = $item->find('css', 'label.gallery-item__checkbox-label.font-icon-tick');
42
        assertNotNull($checkboxLabel, "Could not find checkbox label for file named {$name}");
43
        $checkboxLabel->click();
44
    }
45
46
    /**
47
     * @Then /^I should see the file named "([^"]+)" in the gallery$/
48
     * @param string $name
49
     */
50
    public function iShouldSeeTheGalleryItem($name)
51
    {
52
        $item = $this->getGalleryItem($name);
53
        assertNotNull($item, "File named {$name} could not be found");
0 ignored issues
show
Bug introduced by
The function assertNotNull was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        /** @scrutinizer ignore-call */ 
54
        assertNotNull($item, "File named {$name} could not be found");
Loading history...
54
    }
55
56
    /**
57
     * @Then /^I should not see the file named "([^"]+)" in the gallery$/
58
     * @param string $name
59
     */
60
    public function iShouldNotSeeTheGalleryItem($name)
61
    {
62
        $item = $this->getGalleryItem($name, 0);
63
        assertNull($item, "File named {$name} was found when it should not be visible");
0 ignored issues
show
Bug introduced by
The function assertNull was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        /** @scrutinizer ignore-call */ 
64
        assertNull($item, "File named {$name} was found when it should not be visible");
Loading history...
64
    }
65
66
    /**
67
     * @Then /^I should see the "([^"]*)" form$/
68
     * @param string $id HTML ID of form
69
     * @param integer $timeout
70
     */
71
    public function iShouldSeeTheForm($id, $timeout = 3)
72
    {
73
        /** @var DocumentElement $page */
74
        $page = $this->getMainContext()->getSession()->getPage();
75
        $form = $this->retryThrowable(function () use ($page, $id) {
76
            return $page->find('css', "form#{$id}");
77
        }, $timeout);
78
        assertNotNull($form, "form with id $id could not be found");
0 ignored issues
show
Bug introduced by
The function assertNotNull was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        /** @scrutinizer ignore-call */ 
79
        assertNotNull($form, "form with id $id could not be found");
Loading history...
79
        assertTrue($form->isVisible(), "form with id $id is not visible");
0 ignored issues
show
Bug introduced by
The function assertTrue was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        /** @scrutinizer ignore-call */ 
80
        assertTrue($form->isVisible(), "form with id $id is not visible");
Loading history...
80
    }
81
82
    /**
83
     * @Then /^I should see the file status flag$/
84
     */
85
    public function iShouldSeeTheFileStatusFlag()
86
    {
87
        // TODO This should wait for any XHRs and React rendering to finish
88
        $this->getMainContext()->getSession()->wait(
89
            1000,
90
            "window.jQuery && window.jQuery('.editor__status-flag').size() > 0"
91
        );
92
93
        $page = $this->getMainContext()->getSession()->getPage();
94
        $flag = $page->find('css', '.editor__status-flag');
95
        assertNotNull($flag, "File editor status flag could not be found");
0 ignored issues
show
Bug introduced by
The function assertNotNull was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
        /** @scrutinizer ignore-call */ 
96
        assertNotNull($flag, "File editor status flag could not be found");
Loading history...
96
        assertTrue($flag->isVisible(), "File status flag is not visible");
0 ignored issues
show
Bug introduced by
The function assertTrue was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        /** @scrutinizer ignore-call */ 
97
        assertTrue($flag->isVisible(), "File status flag is not visible");
Loading history...
97
    }
98
99
    /**
100
     * @Then /^I should not see the file status flag$/
101
     */
102
    public function iShouldNotSeeTheFileStatusFlag()
103
    {
104
        // TODO Flakey assertion, since the status flag might not be loaded via XHR yet
105
        $page = $this->getMainContext()->getSession()->getPage();
106
        $flag = $page->find('css', '.editor__status-flag');
107
        assertNull($flag, "File editor status flag should not be present");
0 ignored issues
show
Bug introduced by
The function assertNull was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
        /** @scrutinizer ignore-call */ 
108
        assertNull($flag, "File editor status flag should not be present");
Loading history...
108
    }
109
110
    /**
111
     * @Given /^I click on the latest history item$/
112
     */
113
    public function iClickOnTheLatestHistoryItem()
114
    {
115
        $this->getMainContext()->getSession()->wait(
116
            5000,
117
            "window.jQuery && window.jQuery('.history-list__list li').size() > 0"
118
        );
119
120
        $page = $this->getMainContext()->getSession()->getPage();
121
122
        $elements = $page->find('css', '.history-list__list li');
123
124
        if (null === $elements) {
125
            throw new \InvalidArgumentException(sprintf('Could not find list item'));
126
        }
127
128
        $elements->click();
129
    }
130
131
    /**
132
     * @Given /^I attach the file "([^"]*)" to dropzone "([^"]*)"$/
133
     * @see MinkContext::attachFileToField()
134
     * @param string $path
135
     * @param string $name
136
     */
137
    public function iAttachTheFileToDropzone($path, $name)
138
    {
139
        // Get path
140
        $filesPath = $this->getFilesPath();
141
        if ($filesPath) {
142
            $fullPath = rtrim(realpath($filesPath), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$path;
143
            if (is_file($fullPath)) {
144
                $path = $fullPath;
145
            }
146
        }
147
148
        assertFileExists($path, "$path does not exist");
0 ignored issues
show
Bug introduced by
The function assertFileExists was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
        /** @scrutinizer ignore-call */ 
149
        assertFileExists($path, "$path does not exist");
Loading history...
149
        // Find field
150
        $selector = "input[type=\"file\"].dz-hidden-input.dz-input-{$name}";
151
152
        /** @var DocumentElement $page */
153
        $page = $this->getMainContext()->getSession()->getPage();
154
        $input = $page->find('css', $selector);
155
        assertNotNull($input, "Could not find {$selector}");
0 ignored issues
show
Bug introduced by
The function assertNotNull was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

155
        /** @scrutinizer ignore-call */ 
156
        assertNotNull($input, "Could not find {$selector}");
Loading history...
156
157
        // Make visible temporarily while attaching
158
        $this->getMainContext()->getSession()->executeScript(
159
            <<<EOS
160
window.jQuery('.dz-hidden-input')
161
    .css('visibility', 'visible')
162
    .width(1)
163
    .height(1);
164
EOS
165
        );
166
167
        assert($input->isVisible());
168
        // Attach via html5
169
        $input->attachFile($path);
170
    }
171
172
    /**
173
     * @Then I should see an error message on the file :file
174
     * @param string $file
175
     */
176
    public function iShouldSeeAnErrorMessageOnTheFile($file)
177
    {
178
        $fileNode = $this->getGalleryItem($file);
179
        assertTrue($fileNode->getParent()->hasClass('gallery-item--error'));
0 ignored issues
show
Bug introduced by
The function assertTrue was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

179
        /** @scrutinizer ignore-call */ 
180
        assertTrue($fileNode->getParent()->hasClass('gallery-item--error'));
Loading history...
180
    }
181
182
    /**
183
     * Checks that the message box contains specified text.
184
     *
185
     * @Then /^I should see "(?P<text>(?:[^"]|\\")*)" in the message box$/
186
     * @param string $text
187
     */
188
    public function assertMessageBoxContainsText($text)
189
    {
190
        /** @var FeatureContext $mainContext */
191
        $mainContext = $this->getMainContext();
192
        $mainContext
193
            ->assertSession()
194
            ->elementTextContains('css', '.message-box', str_replace('\\"', '"', $text));
195
    }
196
197
    /**
198
     * Helper for finding items in the visible gallery view
199
     *
200
     * @param string $name Title of item
201
     * @param int $timeout
202
     * @return NodeElement
203
     */
204
    protected function getGalleryItem($name, $timeout = 3)
205
    {
206
        /** @var DocumentElement $page */
207
        $page = $this->getMainContext()->getSession()->getPage();
208
        // Find by cell
209
        $cell = $page->find(
210
            'xpath',
211
            "//div[contains(@class, 'gallery-item')]//div[contains(text(), '{$name}')]"
212
        );
213
        if ($cell) {
214
            return $cell;
215
        }
216
        // Find by row
217
        $row = $page->find(
218
            'xpath',
219
            "//tr[contains(@class, 'gallery__table-row')]//div[contains(text(), '{$name}')]"
220
        );
221
        if ($row) {
222
            return $row;
223
        }
224
        return null;
225
    }
226
227
    /**
228
     * Helper for finding items in the visible gallery view by its order
229
     *
230
     * @param string $rank index of the item to get starting at 1
231
     * @param int $timeout
232
     * @return NodeElement
233
     */
234
    protected function getGalleryItemByRank($rank, $timeout = 3)
235
    {
236
        /** @var DocumentElement $page */
237
        $page = $this->getMainContext()->getSession()->getPage();
238
        // Find by cell
239
        $cell = $page->find(
240
            'xpath',
241
            "//div[contains(@class, 'gallery__files')]/div[$rank]"
242
        );
243
        if ($cell) {
244
            return $cell;
245
        }
246
        // Find by row
247
        $row = $page->find(
248
            'xpath',
249
            "//tr[contains(@class, 'gallery__table-row')][$rank]"
250
        );
251
        if ($row) {
252
            return $row;
253
        }
254
        return null;
255
    }
256
257
    /**
258
     * @Given /^a page "([^"]*)" containing an image "([^"]*)"$/
259
     * @param string $page
260
     * @param string $image
261
     */
262
    public function aPageContaining($page, $image)
263
    {
264
        // Find or create named image
265
        $fields = $this->prepareFixture(Image::class, $image);
266
        /** @var Image $image */
267
        $image = $this->fixtureFactory->createObject(Image::class, $image, $fields);
268
269
        // Create page
270
        $fields = $this->prepareFixture(Page::class, $page);
271
        $fields = array_merge($fields, [
272
            'Title' => $page,
273
            'Content' => sprintf(
274
                '<p>[image id="%d" width="%d" height="%d"]</p>',
275
                $image->ID,
276
                $image->getWidth(),
277
                $image->getHeight()
278
            ),
279
        ]);
280
        $this->fixtureFactory->createObject(Page::class, $page, $fields);
281
    }
282
283
    /**
284
     * @Then I should see a modal titled :title
285
     * @param string $title
286
     */
287
    public function iShouldSeeAModalTitled($title)
288
    {
289
        $page = $this->getMainContext()->getSession()->getPage();
290
        $modalTitle = $page->find('css', '[role=dialog] .modal-header > .modal-title');
291
        assertNotNull($modalTitle, 'No modal on the page');
0 ignored issues
show
Bug introduced by
The function assertNotNull was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

291
        /** @scrutinizer ignore-call */ 
292
        assertNotNull($modalTitle, 'No modal on the page');
Loading history...
292
        assertTrue($modalTitle->getText() == $title);
0 ignored issues
show
Bug introduced by
The function assertTrue was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

292
        /** @scrutinizer ignore-call */ 
293
        assertTrue($modalTitle->getText() == $title);
Loading history...
293
    }
294
295
    /**
296
     * @Then I press the :buttonName button inside the modal
297
     * @param string $buttonName
298
     */
299
    public function iPressButtonInModal($buttonName)
300
    {
301
        $page = $this->getMainContext()->getSession()->getPage();
302
        $modal = $page->find('css', '[role=dialog] .modal-dialog');
303
        assertNotNull($modal, 'No modal on the page');
0 ignored issues
show
Bug introduced by
The function assertNotNull was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

303
        /** @scrutinizer ignore-call */ 
304
        assertNotNull($modal, 'No modal on the page');
Loading history...
304
305
        // Check if the popover is open for the block
306
        $button = $modal->find('xpath', "//button[contains(text(), '$buttonName')]");
307
308
        assertNotNull($button, sprintf('Could not find button labelled "%s"', $buttonName));
309
310
        $button->click();
311
    }
312
313
    /**
314
     * @Then /^I should see the gallery item "([^"]+)" in position "([^"]+)"$/
315
     * @param string $name
316
     * @param string $position
317
     */
318
    public function iShouldSeeTheGalleryItemInPosition($name, $position)
319
    {
320
        $itemByPosition = $this->getGalleryItemByRank($position);
321
        assertNotNull($itemByPosition, 'Should have found a fallery item at position ' . $position);
0 ignored issues
show
Bug introduced by
The function assertNotNull was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

321
        /** @scrutinizer ignore-call */ 
322
        assertNotNull($itemByPosition, 'Should have found a fallery item at position ' . $position);
Loading history...
322
        $title = $itemByPosition->find(
323
            'xpath',
324
            "//div[contains(text(), '{$name}')]"
325
        );
326
        assertNotNull($title, sprintf('File at position %s should be named %s', $position, $name));
327
    }
328
329
    /**
330
     * @When /^I click the "([^"]+)" element$/
331
     * @param $selector
332
     */
333
    public function iClickTheElement($selector)
334
    {
335
        /** @var DocumentElement $page */
336
        $page = $this->getMainContext()->getSession()->getPage();
337
        $element = $page->find('css', $selector);
338
339
        assertNotNull($element, sprintf('Element %s not found', $selector));
0 ignored issues
show
Bug introduced by
The function assertNotNull was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

339
        /** @scrutinizer ignore-call */ 
340
        assertNotNull($element, sprintf('Element %s not found', $selector));
Loading history...
340
341
        $element->click();
342
    }
343
}
344