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

getValidationMessageForImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0

1 Method

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