Completed
Push — master ( cfac17...1a301c )
by Kamil
20:57
created

UpdatePage::getLastImageElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Behat\Page\Admin\Taxon;
13
14
use Behat\Mink\Driver\Selenium2Driver;
15
use Behat\Mink\Element\NodeElement;
16
use Behat\Mink\Exception\ElementNotFoundException;
17
use Sylius\Behat\Behaviour\ChecksCodeImmutability;
18
use Sylius\Behat\Page\Admin\Crud\UpdatePage as BaseUpdatePage;
19
use Sylius\Component\Core\Model\TaxonInterface;
20
use Webmozart\Assert\Assert;
21
22
/**
23
 * @author Arkadiusz Krakowiak <[email protected]>
24
 */
25
class UpdatePage extends BaseUpdatePage implements UpdatePageInterface
26
{
27
    use ChecksCodeImmutability;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function chooseParent(TaxonInterface $taxon)
33
    {
34
        $this->getElement('parent')->selectOption($taxon->getName(), false);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function describeItAs($description, $languageCode)
41
    {
42
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_description', $languageCode), $description);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function nameIt($name, $languageCode)
49
    {
50
        $this->activateLanguageTab($languageCode);
51
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_name', $languageCode), $name);
52
53
        $this->waitForSlugGenerationIfNecessary();
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function specifySlug($slug, $languageCode)
60
    {
61
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_slug', $languageCode), $slug);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function attachImage($path, $type = null)
68
    {
69
        $filesPath = $this->getParameter('files_path');
70
71
        $this->getDocument()->find('css', '[data-form-collection="add"]')->click();
72
73
        $imageForm = $this->getLastImageElement();
74
        if (null !== $type) {
75
            $imageForm->fillField('Type', $type);
76
        }
77
78
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function isImageWithTypeDisplayed($type)
85
    {
86
        $imageElement = $this->getImageElementByType($type);
87
88
        if (null === $imageElement) {
89
            return false;
90
        }
91
92
        $imageUrl = $imageElement->find('css', 'img')->getAttribute('src');
93
        $this->getDriver()->visit($imageUrl);
94
        $pageText = $this->getDocument()->getText();
95
        $this->getDriver()->back();
96
97
        return false === stripos($pageText, '404 Not Found');
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function isSlugReadOnly($languageCode = 'en_US')
104
    {
105
        return 'readonly' === $this->getElement('slug', ['%language%' => $languageCode])->getAttribute('readonly');
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function removeImageWithType($type)
112
    {
113
        $imageElement = $this->getImageElementByType($type);
114
        $imageElement->clickLink('Delete');
115
    }
116
117
    public function removeFirstImage()
118
    {
119
        $imageElement = $this->getFirstImageElement();
120
        $imageElement->clickLink('Delete');
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function enableSlugModification($languageCode = 'en_US')
127
    {
128
        $this->getElement('toggle_taxon_slug_modification_button', ['%locale%' => $languageCode])->press();
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function countImages()
135
    {
136
        $imageElements = $this->getImageElements();
137
138
        return count($imageElements);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function changeImageWithType($type, $path)
145
    {
146
        $filesPath = $this->getParameter('files_path');
147
148
        $imageForm = $this->getImageElementByType($type);
149
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function modifyFirstImageType($type)
156
    {
157
        $firstImage = $this->getFirstImageElement();
158
159
        $typeField = $firstImage->findField('Type');
160
        $typeField->setValue($type);
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function getParent()
167
    {
168
        return $this->getElement('parent')->getValue();
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function getSlug($languageCode = 'en_US')
175
    {
176
        return $this->getElement('slug', ['%language%' => $languageCode])->getValue();
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function getValidationMessageForImage()
183
    {
184
        $lastImageElement = $this->getLastImageElement();
185
186
        $foundElement = $lastImageElement->find('css', '.sylius-validation-error');
187
        if (null === $foundElement) {
188
            throw new ElementNotFoundException($this->getSession(), 'Tag', 'css', '.sylius-validation-error');
189
        }
190
191
        return $foundElement->getText();
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function getValidationMessageForImageAtPlace($place)
198
    {
199
        $images = $this->getImageElements();
200
201
        $foundElement = $images[$place]->find('css', '.sylius-validation-error');
202
        if (null === $foundElement) {
203
            throw new ElementNotFoundException($this->getSession(), 'Tag', 'css', '.sylius-validation-error');
204
        }
205
206
        return $foundElement->getText();
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    public function activateLanguageTab($locale)
213
    {
214
        if (!$this->getDriver() instanceof Selenium2Driver) {
215
            return;
216
        }
217
218
        $languageTabTitle = $this->getElement('language_tab', ['%locale%' => $locale]);
219
        if (!$languageTabTitle->hasClass('active')) {
220
            $languageTabTitle->click();
221
        }
222
223
        $this->getDocument()->waitFor(10, function () use ($languageTabTitle) {
224
            return $languageTabTitle->hasClass('active');
225
        });
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    protected function getElement($name, array $parameters = [])
232
    {
233
        if (!isset($parameters['%language%'])) {
234
            $parameters['%language%'] = 'en_US';
235
        }
236
237
        return parent::getElement($name, $parameters);
238
    }
239
240
    /**
241
     * @return NodeElement
242
     */
243
    protected function getCodeElement()
244
    {
245
        return $this->getElement('code');
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251
    protected function getDefinedElements()
252
    {
253
        return array_merge(parent::getDefinedElements(), [
254
            'code' => '#sylius_taxon_code',
255
            'description' => '#sylius_taxon_translations_en_US_description',
256
            'images' => '#sylius_taxon_images',
257
            'language_tab' => '[data-locale="%locale%"] .title',
258
            'name' => '#sylius_taxon_translations_en_US_name',
259
            'parent' => '#sylius_taxon_parent',
260
            'slug' => '#sylius_taxon_translations_%language%_slug',
261
            'toggle_taxon_slug_modification_button' => '[data-locale="%locale%"] .toggle-taxon-slug-modification',
262
        ]);
263
    }
264
265
    /**
266
     * @return NodeElement
267
     */
268
    private function getLastImageElement()
269
    {
270
        $imageElements = $this->getImageElements();
271
272
        Assert::notEmpty($imageElements);
273
274
        return end($imageElements);
275
    }
276
277
    /**
278
     * @return NodeElement
279
     */
280
    private function getFirstImageElement()
281
    {
282
        $imageElements = $this->getImageElements();
283
284
        Assert::notEmpty($imageElements);
285
286
        return reset($imageElements);
287
    }
288
289
    /**
290
     * @return NodeElement[]
291
     */
292
    private function getImageElements()
293
    {
294
        $images = $this->getElement('images');
295
296
        return $images->findAll('css', 'div[data-form-collection="item"]');
297
    }
298
299
    /**
300
     * @param string $type
301
     *
302
     * @return NodeElement
303
     */
304
    private function getImageElementByType($type)
305
    {
306
        $images = $this->getElement('images');
307
        $typeInput = $images->find('css', 'input[value="'.$type.'"]');
308
309
        if (null === $typeInput) {
310
            return null;
311
        }
312
313
        return $typeInput->getParent()->getParent()->getParent();
314
    }
315
316
    /**
317
     * @param string $languageCode
318
     */
319
    private function waitForSlugGenerationIfNecessary($languageCode = 'en_US')
320
    {
321
        if (!$this->getDriver() instanceof Selenium2Driver) {
322
            return;
323
        }
324
325
        $slugElement = $this->getElement('slug', ['%language%' => $languageCode]);
326
        if ($slugElement->hasAttribute('readonly')) {
327
            return;
328
        }
329
330
        $value = $slugElement->getValue();
331
        $this->getDocument()->waitFor(10, function () use ($slugElement, $value) {
332
            return $value !== $slugElement->getValue();
333
        });
334
    }
335
}
336