Completed
Push — to-be-a-hat-or-not-to-be-kuhwa ( 7c7f7b...b72886 )
by Kamil
20:35
created

UpdateSimpleProductPage::attachImageWithCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
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\Product;
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 Łukasz Chruściel <[email protected]>
24
 */
25
class UpdateSimpleProductPage extends BaseUpdatePage implements UpdateSimpleProductPageInterface
26
{
27
    use ChecksCodeImmutability;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function nameItIn($name, $localeCode)
33
    {
34
        $this->getDocument()->fillField(
35
            sprintf('sylius_product_translations_%s_name', $localeCode), $name
36
        );
37
38
        $this->waitForSlugGenerationIfNecessary();
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function specifyPrice($price)
45
    {
46
        $this->getDocument()->fillField('Price', $price);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getAttributeValue($attribute)
53
    {
54
        $attributesTab = $this->getElement('tab', ['%name%' => 'attributes']);
55
        if (!$attributesTab->hasClass('active')) {
56
            $attributesTab->click();
57
        }
58
59
        return $this->getElement('attribute', ['%attribute%' => $attribute])->getValue();
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function hasAttribute($attribute)
66
    {
67
        return null !== $this->getDocument()->find('css', sprintf('.attribute .label:contains("%s")', $attribute));
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function selectMainTaxon(TaxonInterface $taxon)
74
    {
75
        $this->openTaxonBookmarks();
76
77
        Assert::isInstanceOf($this->getDriver(), Selenium2Driver::class);
78
79
        $this->getDriver()->executeScript(sprintf('$(\'input.search\').val(\'%s\')', $taxon->getName()));
80
        $this->getElement('search')->click();
81
        $this->getElement('search')->waitFor(10,
82
            function () {
83
                return $this->hasElement('search_item_selected');
84
            });
85
        $itemSelected = $this->getElement('search_item_selected');
86
        $itemSelected->click();
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function isMainTaxonChosen($taxonName)
93
    {
94
        $this->openTaxonBookmarks();
95
96
        return $taxonName === $this->getDocument()->find('css', '.search > .text')->getText();
97
    }
98
99
    public function disableTracking()
100
    {
101
        $this->getElement('tracked')->uncheck();
102
    }
103
104
    public function enableTracking()
105
    {
106
        $this->getElement('tracked')->check();
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function isTracked()
113
    {
114
        return $this->getElement('tracked')->isChecked();
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function isImageWithCodeDisplayed($code)
121
    {
122
        $imageElement = $this->getImageElementByCode($code);
123
124
        if (null === $imageElement) {
125
            return false;
126
        }
127
128
        $imageUrl = $imageElement->find('css', 'img')->getAttribute('src');
129
        $this->getDriver()->visit($imageUrl);
130
        $pageText = $this->getDocument()->getText();
131
        $this->getDriver()->back();
132
133
        return false === stripos($pageText, '404 Not Found');
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function attachImage($path, $code = null)
140
    {
141
        $this->clickTabIfItsNotActive('media');
142
143
        $filesPath = $this->getParameter('files_path');
144
145
        $this->getDocument()->clickLink('Add');
146
147
        $imageForm = $this->getLastImageElement();
148
        if (null !== $code) {
149
            $imageForm->fillField('Code', $code);
150
        }
151
152
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function changeImageWithCode($code, $path)
159
    {
160
        $filesPath = $this->getParameter('files_path');
161
162
        $imageForm = $this->getImageElementByCode($code);
163
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function removeImageWithCode($code)
170
    {
171
        $this->clickTabIfItsNotActive('media');
172
173
        $imageElement = $this->getImageElementByCode($code);
174
        $imageElement->clickLink('Delete');
175
    }
176
177
    public function removeFirstImage()
178
    {
179
        $imageElement = $this->getFirstImageElement();
180
        $imageElement->clickLink('Delete');
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function countImages()
187
    {
188
        $imageElements = $this->getImageElements();
189
190
        return count($imageElements);
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196
    public function isImageCodeDisabled()
197
    {
198
        return 'disabled' === $this->getLastImageElement()->findField('Code')->getAttribute('disabled');
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function isSlugReadOnly()
205
    {
206
        return 'readonly' === $this->getElement('slug')->getAttribute('readonly');
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    public function getValidationMessageForImage()
213
    {
214
        $this->clickTabIfItsNotActive('media');
215
216
        $imageForm = $this->getLastImageElement();
217
218
        $foundElement = $imageForm->find('css', '.sylius-validation-error');
219
        if (null === $foundElement) {
220
            throw new ElementNotFoundException($this->getSession(), 'Tag', 'css', '.sylius-validation-error');
221
        }
222
223
        return $foundElement->getText();
224
    }
225
226
    /**
227
     * {@inheritdoc}
228
     */
229
    protected function getCodeElement()
230
    {
231
        return $this->getElement('code');
232
    }
233
234
    /**
235
     * {@inheritdoc}
236
     */
237
    protected function getDefinedElements()
238
    {
239
        return array_merge(parent::getDefinedElements(), [
240
            'attribute' => '.attribute .label:contains("%attribute%") ~ input',
241
            'code' => '#sylius_product_code',
242
            'images' => '#sylius_product_images',
243
            'name' => '#sylius_product_translations_en_US_name',
244
            'price' => '#sylius_product_variant_price',
245
            'search' => '.ui.fluid.search.selection.dropdown',
246
            'search_item_selected' => 'div.menu > div.item.selected',
247
            'slug' => '#sylius_product_translations_en_US_slug',
248
            'tab' => '.menu [data-tab="%name%"]',
249
            'taxonomy' => 'a[data-tab="taxonomy"]',
250
            'tracked' => '#sylius_product_variant_tracked',
251
        ]);
252
    }
253
254
    private function openTaxonBookmarks()
255
    {
256
        $this->getElement('taxonomy')->click();
257
    }
258
259
    /**
260
     * @param string $tabName
261
     */
262
    private function clickTabIfItsNotActive($tabName)
263
    {
264
        $attributesTab = $this->getElement('tab', ['%name%' => $tabName]);
265
        if (!$attributesTab->hasClass('active')) {
266
            $attributesTab->click();
267
        }
268
    }
269
270
    /**
271
     * @param string $code
272
     *
273
     * @return NodeElement
274
     */
275
    private function getImageElementByCode($code)
276
    {
277
        $images = $this->getElement('images');
278
        $inputCode = $images->find('css', 'input[value="'.$code.'"]');
279
280
        if (null === $inputCode) {
281
            return null;
282
        }
283
284
        return $inputCode->getParent()->getParent()->getParent();
285
    }
286
287
    /**
288
     * @return NodeElement[]
289
     */
290
    private function getImageElements()
291
    {
292
        $images = $this->getElement('images');
293
294
        return $images->findAll('css', 'div[data-form-collection="item"]');
295
    }
296
297
    /**
298
     * @return NodeElement
299
     */
300
    private function getLastImageElement()
301
    {
302
        $imageElements = $this->getImageElements();
303
304
        Assert::notEmpty($imageElements);
305
306
        return end($imageElements);
307
    }
308
309
    /**
310
     * @return NodeElement
311
     */
312
    private function getFirstImageElement()
313
    {
314
        $imageElements = $this->getImageElements();
315
316
        Assert::notEmpty($imageElements);
317
318
        return reset($imageElements);
319
    }
320
321
    private function waitForSlugGenerationIfNecessary()
322
    {
323
        if (!$this->getDriver() instanceof Selenium2Driver) {
324
            return;
325
        }
326
327
        $slugElement = $this->getElement('slug');
328
        if ($slugElement->hasAttribute('readonly')) {
329
            return;
330
        }
331
332
        $value = $slugElement->getValue();
333
        $this->getDocument()->waitFor(1000, function () use ($slugElement, $value) {
334
            return $value !== $slugElement->getValue();
335
        });
336
    }
337
}
338