Completed
Push — master ( 7c0075...645752 )
by Kamil
18:52
created

UpdatePage   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 306
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 36
c 0
b 0
f 0
lcom 1
cbo 8
dl 0
loc 306
rs 8.8

26 Methods

Rating   Name   Duplication   Size   Complexity  
A chooseParent() 0 4 1
A describeItAs() 0 4 1
A nameIt() 0 7 1
A specifySlug() 0 4 1
A attachImage() 0 13 2
A isImageWithCodeDisplayed() 0 15 2
A isSlugReadOnly() 0 4 1
A removeImageWithCode() 0 5 1
A removeFirstImage() 0 5 1
A enableSlugModification() 0 4 1
A countImages() 0 6 1
A changeImageWithCode() 0 7 1
A getParent() 0 4 1
A getSlug() 0 4 1
A getValidationMessageForImage() 0 11 2
A getValidationMessageForImageAtPlace() 0 11 2
A isImageCodeDisabled() 0 4 1
A activateLanguageTab() 0 15 3
A getElement() 0 8 2
A getCodeElement() 0 4 1
A getDefinedElements() 0 13 1
A getLastImageElement() 0 8 1
A getFirstImageElement() 0 6 1
A getImageElements() 0 6 1
A getImageElementByCode() 0 11 2
A waitForSlugGenerationIfNecessary() 0 16 3
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, $code = 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 !== $code) {
75
            $imageForm->fillField('Code', $code);
76
        }
77
78
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function isImageWithCodeDisplayed($code)
85
    {
86
        $imageElement = $this->getImageElementByCode($code);
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 removeImageWithCode($code)
112
    {
113
        $imageElement = $this->getImageElementByCode($code);
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 changeImageWithCode($code, $path)
145
    {
146
        $filesPath = $this->getParameter('files_path');
147
148
        $imageForm = $this->getImageElementByCode($code);
149
        $imageForm->find('css', 'input[type="file"]')->attachFile($filesPath.$path);
150
    }
151
152
    /**
153
     * @inheritDoc
154
     */
155
    public function getParent()
156
    {
157
        return $this->getElement('parent')->getValue();
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function getSlug($languageCode = 'en_US')
164
    {
165
        return $this->getElement('slug', ['%language%' => $languageCode])->getValue();
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function getValidationMessageForImage()
172
    {
173
        $provinceForm = $this->getLastImageElement();
174
175
        $foundElement = $provinceForm->find('css', '.sylius-validation-error');
176
        if (null === $foundElement) {
177
            throw new ElementNotFoundException($this->getSession(), 'Tag', 'css', '.sylius-validation-error');
178
        }
179
180
        return $foundElement->getText();
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function getValidationMessageForImageAtPlace($place) {
187
        
188
        $images = $this->getImageElements();
189
        
190
        $foundElement = $images[$place]->find('css', '.sylius-validation-error');
191
        if (null === $foundElement) {
192
            throw new ElementNotFoundException($this->getSession(), 'Tag', 'css', '.sylius-validation-error');
193
        }
194
195
        return $foundElement->getText();
196
    }
197
198
    /**
199
     * @return bool
200
     */
201
    public function isImageCodeDisabled()
202
    {
203
        return 'disabled' === $this->getLastImageElement()->findField('Code')->getAttribute('disabled');
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    public function activateLanguageTab($locale)
210
    {
211
        if (!$this->getDriver() instanceof Selenium2Driver) {
212
            return;
213
        }
214
215
        $languageTabTitle = $this->getElement('language_tab', ['%locale%' => $locale]);
216
        if (!$languageTabTitle->hasClass('active')) {
217
            $languageTabTitle->click();
218
        }
219
220
        $this->getDocument()->waitFor(10, function () use ($languageTabTitle) {
221
            return $languageTabTitle->hasClass('active');
222
        });
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228
    protected function getElement($name, array $parameters = [])
229
    {
230
        if (!isset($parameters['%language%'])) {
231
            $parameters['%language%'] = 'en_US';
232
        }
233
234
        return parent::getElement($name, $parameters);
235
    }
236
237
    /**
238
     * @return NodeElement
239
     */
240
    protected function getCodeElement()
241
    {
242
        return $this->getElement('code');
243
    }
244
245
    /**
246
     * {@inheritdoc}
247
     */
248
    protected function getDefinedElements()
249
    {
250
        return array_merge(parent::getDefinedElements(), [
251
            'code' => '#sylius_taxon_code',
252
            'description' => '#sylius_taxon_translations_en_US_description',
253
            'images' => '#sylius_taxon_images',
254
            'language_tab' => '[data-locale="%locale%"] .title',
255
            'name' => '#sylius_taxon_translations_en_US_name',
256
            'parent' => '#sylius_taxon_parent',
257
            'slug' => '#sylius_taxon_translations_%language%_slug',
258
            'toggle_taxon_slug_modification_button' => '[data-locale="%locale%"] .toggle-taxon-slug-modification',
259
        ]);
260
    }
261
262
    /**
263
     * @return NodeElement
264
     */
265
    private function getLastImageElement()
266
    {
267
        $imageElements = $this->getImageElements();
268
269
        Assert::notEmpty($imageElements);
270
271
        return end($imageElements);
272
    }
273
274
    /**
275
     * @return NodeElement
276
     */
277
    private function getFirstImageElement()
278
    {
279
        $imageElements = $this->getImageElements();
280
281
        return reset($imageElements);
282
    }
283
284
    /**
285
     * @return NodeElement[]
286
     */
287
    private function getImageElements()
288
    {
289
        $images = $this->getElement('images');
290
291
        return $images->findAll('css', 'div[data-form-collection="item"]');
292
    }
293
294
    /**
295
     * @param string $code
296
     *
297
     * @return NodeElement
298
     */
299
    private function getImageElementByCode($code)
300
    {
301
        $images = $this->getElement('images');
302
        $inputCode = $images->find('css', 'input[value="'.$code.'"]');
303
304
        if (null === $inputCode) {
305
            return null;
306
        }
307
308
        return $inputCode->getParent()->getParent()->getParent();
309
    }
310
311
    /**
312
     * @param string $languageCode
313
     */
314
    private function waitForSlugGenerationIfNecessary($languageCode = 'en_US')
315
    {
316
        if (!$this->getDriver() instanceof Selenium2Driver) {
317
            return;
318
        }
319
320
        $slugElement = $this->getElement('slug', ['%language%' => $languageCode]);
321
        if ($slugElement->hasAttribute('readonly')) {
322
            return;
323
        }
324
325
        $value = $slugElement->getValue();
326
        $this->getDocument()->waitFor(10, function () use ($slugElement, $value) {
327
            return $value !== $slugElement->getValue();
328
        });
329
    }
330
}
331