Completed
Pull Request — master (#443)
by Damian
02:15
created

FixtureContext::getGalleryItem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 9.2
cc 3
eloc 13
nc 3
nop 2
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 Page;
8
use SilverStripe\Assets\Image;
9
use SilverStripe\BehatExtension\Context\FixtureContext as BaseFixtureContext;
10
use SilverStripe\BehatExtension\Utility\StepHelper;
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 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");
29
        $item->click();
30
    }
31
32
    /**
33
     * Check the checkbox for a given gallery item
34
     * @Given /^I check the file 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");
41
        $checkbox = $item->find('css', 'input[type="checkbox"]');
42
        assertNotNull($checkbox, "Could not find checkbox for file named {$name}");
43
        $checkbox->check();
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");
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");
64
    }
65
66
    /**
67
     * @Then /^I should see the "([^"]*)" form$/
68
     * @param string $id HTML ID of form
69
     */
70 View Code Duplication
    public function iShouldSeeTheForm($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
71
    {
72
        /** @var DocumentElement $page */
73
        $page = $this->getMainContext()->getSession()->getPage();
74
        $form = $page->find('css', "form#{$id}");
75
        assertNotNull($form, "form with id $id could not be found");
76
        assertTrue($form->isVisible(), "form with id $id is not visible");
77
    }
78
79
    /**
80
     * @Then /^I should see the file status flag$/
81
     */
82 View Code Duplication
    public function iShouldSeeTheFileStatusFlag()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
83
    {
84
        $page = $this->getMainContext()->getSession()->getPage();
85
        $flag = $page->find('css', '.editor__status-flag');
86
        assertNotNull($flag, "File editor status flag could not be found");
87
        assertTrue($flag->isVisible(), "File status flag is not visible");
88
    }
89
90
    /**
91
     * @Then /^I should not see the file status flag$/
92
     */
93
    public function iShouldNotSeeTheFileStatusFlag()
94
    {
95
        $page = $this->getMainContext()->getSession()->getPage();
96
        $flag = $page->find('css', '.editor__status-flag');
97
        assertNull($flag, "File editor status flag should not be present");
98
    }
99
100
    /**
101
     * @Given /^I click on the latest history item$/
102
     */
103
    public function iClickOnTheLatestHistoryItem()
104
    {
105
        $this->getMainContext()->getSession()->wait(
106
            5000,
107
            "window.jQuery && window.jQuery('.file-history__list li').size() > 0"
108
        );
109
110
        $page = $this->getMainContext()->getSession()->getPage();
111
112
        $elements = $page->find('css', '.file-history__list li');
113
114
        if (null === $elements) {
115
            throw new \InvalidArgumentException(sprintf('Could not find list item'));
116
        }
117
118
        $elements->click();
119
    }
120
121
    /**
122
     * @Given /^I attach the file "([^"]*)" to dropzone "([^"]*)"$/
123
     * @see MinkContext::attachFileToField()
124
     * @param string $path
125
     * @param string $name
126
     */
127
    public function iAttachTheFileToDropzone($path, $name)
128
    {
129
        // Get path
130
        $filesPath = $this->getFilesPath();
131
        if ($filesPath) {
132
            $fullPath = rtrim(realpath($filesPath), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$path;
133
            if (is_file($fullPath)) {
134
                $path = $fullPath;
135
            }
136
        }
137
138
        assertFileExists($path, "$path does not exist");
139
        // Find field
140
        $selector = "input[type=\"file\"].dz-hidden-input.dz-input-{$name}";
141
142
        /** @var DocumentElement $page */
143
        $page = $this->getMainContext()->getSession()->getPage();
144
        $input = $page->find('css', $selector);
145
        assertNotNull($input, "Could not find {$selector}");
146
147
        // Make visible temporarily while attaching
148
        $this->getMainContext()->getSession()->executeScript(
149
            <<<EOS
150
window.jQuery('.dz-hidden-input')
151
    .css('visibility', 'visible')
152
    .width(1)
153
    .height(1);
154
EOS
155
        );
156
157
        assert($input->isVisible());
158
        // Attach via html5
159
        $input->attachFile($path);
160
    }
161
162
    /**
163
     * Checks that the message box contains specified text.
164
     *
165
     * @Then /^I should see "(?P<text>(?:[^"]|\\")*)" in the message box$/
166
     * @param string $text
167
     */
168
    public function assertMessageBoxContainsText($text)
169
    {
170
        /** @var FeatureContext $mainContext */
171
        $mainContext = $this->getMainContext();
172
        $mainContext
173
            ->assertSession()
174
            ->elementTextContains('css', '.message-box', str_replace('\\"', '"', $text));
175
    }
176
177
    /**
178
     * Helper for finding items in the visible gallery view
179
     *
180
     * @param string $name Title of item
181
     * @param int $timeout
182
     * @return NodeElement
183
     */
184
    protected function getGalleryItem($name, $timeout = 3)
0 ignored issues
show
Unused Code introduced by
The parameter $timeout is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
185
    {
186
        /** @var DocumentElement $page */
187
        $page = $this->getMainContext()->getSession()->getPage();
188
        // Find by cell
189
        $cell = $page->find(
190
            'xpath',
191
            "//div[contains(@class, 'gallery-item')]//div[contains(text(), '{$name}')]"
192
        );
193
        if ($cell) {
194
            return $cell;
195
        }
196
        // Find by row
197
        $row = $page->find(
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $row is correct as $page->find('xpath', "//...s(text(), '{$name}')]") (which targets Behat\Mink\Element\Element::find()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
198
            'xpath',
199
            "//tr[contains(@class, 'gallery__table-row')]//div[contains(text(), '{$name}')]"
200
        );
201
        if ($row) {
202
            return $row;
203
        }
204
        return null;
205
    }
206
207
    /**
208
     * @Given /^a page "([^"]*)" containing an image "([^"]*)"$/
209
     * @param string $page
210
     * @param string $image
211
     */
212
    public function aPageContaining($page, $image)
213
    {
214
        // Find or create named image
215
        $fields = $this->prepareFixture(Image::class, $image);
216
        /** @var Image $image */
217
        $image = $this->fixtureFactory->createObject(Image::class, $image, $fields);
218
219
        // Create page
220
        $fields = $this->prepareFixture(Page::class, $page);
221
        $fields = array_merge($fields, [
222
            'Title' => $page,
223
            'Content' => sprintf(
224
                '<p>[image id="%d" width="%d" height="%d"]</p>',
225
                $image->ID,
226
                $image->getWidth(),
227
                $image->getHeight()
228
            ),
229
        ]);
230
        $this->fixtureFactory->createObject(Page::class, $page, $fields);
231
    }
232
}
233