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

UpdateConfigurableProductPage::attachImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
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\Taxonomy\Model\TaxonInterface;
20
use Webmozart\Assert\Assert;
21
22
/**
23
 * @author Łukasz Chruściel <[email protected]>
24
 */
25
class UpdateConfigurableProductPage extends BaseUpdatePage implements UpdateConfigurableProductPageInterface
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
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function isProductOptionChosen($option)
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...
43
    {
44
        return $this->getElement('options')->find('named', array('option', $option))->hasAttribute('selected');
45
    }
46
47
    /**
48
     * @return bool
49
     */
50
    public function isProductOptionsDisabled()
51
    {
52
        return 'disabled' === $this->getElement('options')->getAttribute('disabled');
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function isMainTaxonChosen($taxonName)
59
    {
60
        $this->openTaxonBookmarks();
61
        Assert::notNull($this->getDocument()->find('css', '.search > .text'));
62
63
        return $taxonName === $this->getDocument()->find('css', '.search > .text')->getText();
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function selectMainTaxon(TaxonInterface $taxon)
70
    {
71
        $this->openTaxonBookmarks();
72
        
73
        Assert::isInstanceOf($this->getDriver(), Selenium2Driver::class);
74
        
75
        $this->getDriver()->executeScript(sprintf('$(\'input.search\').val(\'%s\')', $taxon->getName()));
76
        $this->getElement('search')->click();
77
        $this->getElement('search')->waitFor(10, function () {
78
                return $this->hasElement('search_item_selected');
79
        });
80
        $itemSelected = $this->getElement('search_item_selected');
81
        $itemSelected->click();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function isImageWithCodeDisplayed($code)
88
    {
89
        $imageElement = $this->getImageElementByCode($code);
90
91
        if (null === $imageElement) {
92
            return false;
93
        }
94
95
        $imageUrl = $imageElement->find('css', 'img')->getAttribute('src');
96
        $this->getDriver()->visit($imageUrl);
97
        $pageText = $this->getDocument()->getText();
98
        $this->getDriver()->back();
99
100
        return false === stripos($pageText, '404 Not Found');
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function attachImage($path, $code = null)
107
    {
108
        $this->clickTabIfItsNotActive('media');
109
110
        $filesPath = $this->getParameter('files_path');
111
112
        $this->getDocument()->clickLink('Add');
113
114
        $imageForm = $this->getLastImageElement();
115
        if (null !== $code) {
116
            $imageForm->fillField('Code', $code);
117
        }
118
119
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function changeImageWithCode($code, $path)
126
    {
127
        $filesPath = $this->getParameter('files_path');
128
129
        $imageForm = $this->getImageElementByCode($code);
130
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function removeImageWithCode($code)
137
    {
138
        $this->clickTabIfItsNotActive('media');
139
140
        $imageElement = $this->getImageElementByCode($code);
141
        $imageElement->clickLink('Delete');
142
    }
143
144
    public function removeFirstImage()
145
    {
146
        $imageElement = $this->getFirstImageElement();
147
        $imageElement->clickLink('Delete');
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function countImages()
154
    {
155
        $imageElements = $this->getImageElements();
156
157
        return count($imageElements);
158
    }
159
160
    /**
161
     * @return bool
162
     */
163
    public function isImageCodeDisabled()
164
    {
165
        return 'disabled' === $this->getLastImageElement()->findField('Code')->getAttribute('disabled');
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function getValidationMessageForImage()
172
    {
173
        $this->clickTabIfItsNotActive('media');
174
175
        $imageForm = $this->getLastImageElement();
176
177
        $foundElement = $imageForm->find('css', '.sylius-validation-error');
178
        if (null === $foundElement) {
179
            throw new ElementNotFoundException($this->getSession(), 'Tag', 'css', '.sylius-validation-error');
180
        }
181
182
        return $foundElement->getText();
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    protected function getCodeElement()
189
    {
190
        return $this->getElement('code');
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196
    protected function getDefinedElements()
197
    {
198
        return array_merge(parent::getDefinedElements(), [
199
            'code' => '#sylius_product_code',
200
            'images' => '#sylius_product_images',
201
            'name' => '#sylius_product_translations_en_US_name',
202
            'options' => '#sylius_product_options',
203
            'price' => '#sylius_product_variant_price',
204
            'search' => '.ui.fluid.search.selection.dropdown',
205
            'search_item_selected' => 'div.menu > div.item.selected',
206
            'tab' => '.menu [data-tab="%name%"]',
207
            'taxonomy' => 'a[data-tab="taxonomy"]',
208
        ]);
209
    }
210
211
    private function openTaxonBookmarks()
212
    {
213
        $this->getElement('taxonomy')->click();
214
    }
215
216
    /**
217
     * @param string $tabName
218
     */
219
    private function clickTabIfItsNotActive($tabName)
220
    {
221
        $attributesTab = $this->getElement('tab', ['%name%' => $tabName]);
222
        if (!$attributesTab->hasClass('active')) {
223
            $attributesTab->click();
224
        }
225
    }
226
227
    /**
228
     * @param string $code
229
     *
230
     * @return NodeElement
231
     */
232
    private function getImageElementByCode($code)
233
    {
234
        $images = $this->getElement('images');
235
        $inputCode = $images->find('css', 'input[value="'.$code.'"]');
236
237
        if (null === $inputCode) {
238
            return null;
239
        }
240
241
        return $inputCode->getParent()->getParent()->getParent();
242
    }
243
244
    /**
245
     * @return NodeElement[]
246
     */
247
    private function getImageElements()
248
    {
249
        $images = $this->getElement('images');
250
251
        return $images->findAll('css', 'div[data-form-collection="item"]');
252
    }
253
254
    /**
255
     * @return NodeElement
256
     */
257
    private function getLastImageElement()
258
    {
259
        $imageElements = $this->getImageElements();
260
261
        Assert::notEmpty($imageElements);
262
263
        return end($imageElements);
264
    }
265
266
    /**
267
     * @return NodeElement
268
     */
269
    private function getFirstImageElement()
270
    {
271
        $imageElements = $this->getImageElements();
272
273
        Assert::notEmpty($imageElements);
274
275
        return reset($imageElements);
276
    }
277
}
278