Completed
Push — 1.0-reliable-behat ( 67387b )
by Kamil
55:32 queued 21:17
created

UpdatePage::isImageWithTypeDisplayed()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 4
nop 1
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
declare(strict_types=1);
13
14
namespace Sylius\Behat\Page\Admin\Taxon;
15
16
use Behat\Mink\Driver\Selenium2Driver;
17
use Behat\Mink\Element\NodeElement;
18
use Behat\Mink\Exception\ElementNotFoundException;
19
use Sylius\Behat\Behaviour\ChecksCodeImmutability;
20
use Sylius\Behat\Page\Admin\Crud\UpdatePage as BaseUpdatePage;
21
use Sylius\Behat\Service\AutocompleteHelper;
22
use Sylius\Behat\Service\SlugGenerationHelper;
23
use Sylius\Component\Core\Model\TaxonInterface;
24
use Webmozart\Assert\Assert;
25
26
class UpdatePage extends BaseUpdatePage implements UpdatePageInterface
27
{
28
    use ChecksCodeImmutability;
29
30
    /** @var array */
31
    private $imageUrls = [];
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function chooseParent(TaxonInterface $taxon)
37
    {
38
        AutocompleteHelper::chooseValue($this->getSession(), $this->getElement('parent')->getParent(), $taxon->getName());
0 ignored issues
show
Bug introduced by
It seems like $this->getElement('parent')->getParent() can be null; however, chooseValue() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function describeItAs($description, $languageCode)
45
    {
46
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_description', $languageCode), $description);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function nameIt($name, $languageCode)
53
    {
54
        $this->activateLanguageTab($languageCode);
55
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_name', $languageCode), $name);
56
57
        if ($this->getDriver() instanceof Selenium2Driver) {
58
            SlugGenerationHelper::waitForSlugGeneration(
59
                $this->getSession(),
60
                $this->getElement('slug', ['%language%' => $languageCode])
61
            );
62
        }
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function specifySlug($slug, $languageCode)
69
    {
70
        $this->getDocument()->fillField(sprintf('sylius_taxon_translations_%s_slug', $languageCode), $slug);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function attachImage($path, $type = null)
77
    {
78
        $filesPath = $this->getParameter('files_path');
79
80
        $this->getDocument()->find('css', '[data-form-collection="add"]')->click();
81
82
        $imageForm = $this->getLastImageElement();
83
        if (null !== $type) {
84
            $imageForm->fillField('Type', $type);
85
        }
86
87
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath . $path);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function isImageWithTypeDisplayed($type)
94
    {
95
        $imageElement = $this->getImageElementByType($type);
96
97
        var_dump($imageElement ? $imageElement->find('css', 'img')->getAttribute('src') : 'null');
0 ignored issues
show
Security Debugging Code introduced by
var_dump($imageElement ?...ibute('src') : 'null'); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
98
        var_dump($this->provideImageUrlForType($type));
99
100
        $imageUrl = $imageElement ? $imageElement->find('css', 'img')->getAttribute('src') : $this->provideImageUrlForType($type);
101
        if (null === $imageElement && null === $imageUrl) {
102
            return false;
103
        }
104
105
        $this->getDriver()->visit($imageUrl);
106
        var_dump($imageUrl);
107
        $pageText = $this->getDocument()->getText();
108
        $this->getDriver()->back();
109
110
        return false === stripos($pageText, '404 Not Found');
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function isSlugReadonly($languageCode = 'en_US')
117
    {
118
        return SlugGenerationHelper::isSlugReadonly(
119
            $this->getSession(),
120
            $this->getElement('slug', ['%language%' => $languageCode])
121
        );
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function removeImageWithType($type)
128
    {
129
        $imageElement = $this->getImageElementByType($type);
130
        $imageSourceElement = $imageElement->find('css', 'img');
131
        if (null !== $imageSourceElement) {
132
            $this->saveImageUrlForType($type, $imageSourceElement->getAttribute('src'));
133
        }
134
135
        $imageElement->clickLink('Delete');
136
    }
137
138
    public function removeFirstImage()
139
    {
140
        $imageElement = $this->getFirstImageElement();
141
        $imageTypeElement = $imageElement->find('css', 'input[type=text]');
142
        $imageSourceElement = $imageElement->find('css', 'img');
143
144
        if (null !== $imageTypeElement && null !== $imageSourceElement) {
145
            $this->saveImageUrlForType(
146
                $imageTypeElement->getValue(),
147
                $imageSourceElement->getAttribute('src')
148
            );
149
        }
150
151
        $imageElement->clickLink('Delete');
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function enableSlugModification($languageCode = 'en_US')
158
    {
159
        SlugGenerationHelper::enableSlugModification(
160
            $this->getSession(),
161
            $this->getElement('toggle_taxon_slug_modification_button', ['%locale%' => $languageCode])
162
        );
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function countImages()
169
    {
170
        $imageElements = $this->getImageElements();
171
172
        return count($imageElements);
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function changeImageWithType($type, $path)
179
    {
180
        $filesPath = $this->getParameter('files_path');
181
182
        $imageForm = $this->getImageElementByType($type);
183
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath . $path);
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function modifyFirstImageType($type)
190
    {
191
        $firstImage = $this->getFirstImageElement();
192
193
        $typeField = $firstImage->findField('Type');
194
        $typeField->setValue($type);
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200
    public function getParent()
201
    {
202
        return $this->getElement('parent')->getValue();
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208
    public function getSlug($languageCode = 'en_US')
209
    {
210
        return $this->getElement('slug', ['%language%' => $languageCode])->getValue();
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216
    public function getValidationMessageForImage()
217
    {
218
        $lastImageElement = $this->getLastImageElement();
219
220
        $foundElement = $lastImageElement->find('css', '.sylius-validation-error');
221
        if (null === $foundElement) {
222
            throw new ElementNotFoundException($this->getSession(), 'Tag', 'css', '.sylius-validation-error');
223
        }
224
225
        return $foundElement->getText();
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function getValidationMessageForImageAtPlace($place)
232
    {
233
        $images = $this->getImageElements();
234
235
        $foundElement = $images[$place]->find('css', '.sylius-validation-error');
236
        if (null === $foundElement) {
237
            throw new ElementNotFoundException($this->getSession(), 'Tag', 'css', '.sylius-validation-error');
238
        }
239
240
        return $foundElement->getText();
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     */
246
    public function activateLanguageTab($locale)
247
    {
248
        if (!$this->getDriver() instanceof Selenium2Driver) {
249
            return;
250
        }
251
252
        $languageTabTitle = $this->getElement('language_tab', ['%locale%' => $locale]);
253
        if (!$languageTabTitle->hasClass('active')) {
254
            $languageTabTitle->click();
255
        }
256
257
        $this->getDocument()->waitFor(10, function () use ($languageTabTitle) {
258
            return $languageTabTitle->hasClass('active');
259
        });
260
    }
261
262
    /**
263
     * {@inheritdoc}
264
     */
265
    protected function getElement($name, array $parameters = [])
266
    {
267
        if (!isset($parameters['%language%'])) {
268
            $parameters['%language%'] = 'en_US';
269
        }
270
271
        return parent::getElement($name, $parameters);
272
    }
273
274
    /**
275
     * @return NodeElement
276
     */
277
    protected function getCodeElement()
278
    {
279
        return $this->getElement('code');
280
    }
281
282
    /**
283
     * {@inheritdoc}
284
     */
285
    protected function getDefinedElements()
286
    {
287
        return array_merge(parent::getDefinedElements(), [
288
            'code' => '#sylius_taxon_code',
289
            'description' => '#sylius_taxon_translations_en_US_description',
290
            'images' => '#sylius_taxon_images',
291
            'language_tab' => '[data-locale="%locale%"] .title',
292
            'name' => '#sylius_taxon_translations_en_US_name',
293
            'parent' => '#sylius_taxon_parent',
294
            'slug' => '#sylius_taxon_translations_%language%_slug',
295
            'toggle_taxon_slug_modification_button' => '[data-locale="%locale%"] .toggle-taxon-slug-modification',
296
        ]);
297
    }
298
299
    /**
300
     * @return NodeElement
301
     */
302
    private function getLastImageElement()
303
    {
304
        $imageElements = $this->getImageElements();
305
306
        Assert::notEmpty($imageElements);
307
308
        return end($imageElements);
309
    }
310
311
    /**
312
     * @return NodeElement
313
     */
314
    private function getFirstImageElement()
315
    {
316
        $imageElements = $this->getImageElements();
317
318
        Assert::notEmpty($imageElements);
319
320
        return reset($imageElements);
321
    }
322
323
    /**
324
     * @return NodeElement[]
325
     */
326
    private function getImageElements()
327
    {
328
        $images = $this->getElement('images');
329
330
        return $images->findAll('css', 'div[data-form-collection="item"]');
331
    }
332
333
    /**
334
     * @param string $type
335
     *
336
     * @return NodeElement
337
     */
338
    private function getImageElementByType($type)
339
    {
340
        $images = $this->getElement('images');
341
        $typeInput = $images->find('css', 'input[value="' . $type . '"]');
342
343
        if (null === $typeInput) {
344
            return null;
345
        }
346
347
        return $typeInput->getParent()->getParent()->getParent();
348
    }
349
350
    private function provideImageUrlForType(string $type): ?string
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
351
    {
352
        return $this->imageUrls[$type] ?? null;
353
    }
354
355
    private function saveImageUrlForType(string $type, string $imageUrl): void
356
    {
357
        if (false !== strpos($imageUrl, 'data:image/jpeg')) {
358
            return;
359
        }
360
361
        $this->imageUrls[$type] = $imageUrl;
362
    }
363
}
364