Passed
Pull Request — master (#677)
by Aleksandr
07:54
created

CmsGuiPresentationTester::openIboxForElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types = 1);
9
10
namespace PyzTest\Zed\CmsGui;
11
12
use Codeception\Actor;
13
use Exception;
14
use Faker\Factory;
15
16
/**
17
 * Inherited Methods
18
 *
19
 * @method void wantToTest($text)
20
 * @method void wantTo($text)
21
 * @method void execute($callable)
22
 * @method void expectTo($prediction)
23
 * @method void expect($prediction)
24
 * @method void amGoingTo($argumentation)
25
 * @method void am($role)
26
 * @method void lookForwardTo($achieveValue)
27
 * @method void comment($description)
28
 * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL)
29
 *
30
 * @SuppressWarnings(\PyzTest\Zed\CmsGui\PHPMD)
31
 */
32
class CmsGuiPresentationTester extends Actor
33
{
34
    use _generated\CmsGuiPresentationTesterActions;
0 ignored issues
show
Bug introduced by
The type PyzTest\Zed\CmsGui\_gene...esentationTesterActions was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
36
    /**
37
     * @var array|null
38
     */
39
    protected $localizedFakeData;
40
41
    /**
42
     * @param string $date
43
     *
44
     * @return $this
45
     */
46
    public function setValidFrom(string $date)
47
    {
48
        $date = $this->adaptDateInputForBrowser($date);
49
        $this->fillField('//*[@id="cms_page_validFrom"]', $date);
50
51
        return $this;
52
    }
53
54
    /**
55
     * @return $this
56
     */
57
    public function setIsSearchable()
58
    {
59
        $this->checkOption('//*[@id="cms_page_isSearchable"]');
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param string $date
66
     *
67
     * @return $this
68
     */
69
    public function setValidTo(string $date)
70
    {
71
        $date = $this->adaptDateInputForBrowser($date);
72
        $this->fillField('//*[@id="cms_page_validTo"]', $date);
73
74
        return $this;
75
    }
76
77
    /**
78
     * @param string $selector
79
     *
80
     * @return bool
81
     */
82
    public function tryToSeeElement(string $selector): bool
83
    {
84
        try {
85
            $this->seeElement($selector);
86
87
            return true;
88
        } catch (Exception $e) {
89
            return false;
90
        }
91
    }
92
93
    /**
94
     * @param string $elementIdentifier
95
     *
96
     * @return $this
97
     */
98
    public function openIboxForElement(string $elementIdentifier)
99
    {
100
        $collapseLinkIdentifier = $elementIdentifier
101
            . '/ancestor::div[contains(@class, "ibox") and contains(@class, "collapsed")]//a[@class="collapse-link"]';
102
103
        $this->waitForElementClickable($collapseLinkIdentifier);
104
105
        $this->click($collapseLinkIdentifier);
106
107
        return $this;
108
    }
109
110
    /**
111
     * @return $this
112
     */
113
    protected function makeSureCalendarIsClosed()
114
    {
115
        if ($this->tryToSeeElement('//*[@id="ui-datepicker-div"]')) {
116
            $this->click('//*[@class="footer"]');
117
        }
118
119
        return $this;
120
    }
121
122
    /**
123
     * @param int $formIndex
124
     * @param string $name
125
     * @param string $url
126
     *
127
     * @return $this
128
     */
129
    public function fillLocalizedUrlForm(int $formIndex, string $name, string $url)
130
    {
131
        $this->makeSureCalendarIsClosed();
132
133
        $nameFieldIdentifier = sprintf('//*[@id="cms_page_pageAttributes_%s_name"]', $formIndex);
134
135
        if (!$this->tryToSeeElement($nameFieldIdentifier)) {
136
            $this->openIboxForElement($nameFieldIdentifier);
137
        }
138
139
        $this->waitForElementVisible($nameFieldIdentifier);
140
141
        $this->fillField($nameFieldIdentifier, $name);
142
143
        $urlFieldIdentifier = sprintf('//*[@id="cms_page_pageAttributes_%s_url"]', $formIndex);
144
145
        if (!$this->tryToSeeElement($urlFieldIdentifier)) {
146
            $this->openIboxForElement($urlFieldIdentifier);
147
        }
148
149
        $this->waitForElementVisible($urlFieldIdentifier);
150
        $this->fillField($urlFieldIdentifier, $url);
151
152
        return $this;
153
    }
154
155
    /**
156
     * @param int $placeHolderIndex
157
     * @param int $localeIndex
158
     * @param string $contents
159
     *
160
     * @return void
161
     */
162
    public function fillPlaceholderContents(int $placeHolderIndex, int $localeIndex, string $contents): void
163
    {
164
        $translationElementId = 'cms_glossary_glossaryAttributes_' . $placeHolderIndex . '_translations_' . $localeIndex . '_translation';
165
166
        $this->executeJS("$('#$translationElementId').text('$contents');");
167
    }
168
169
    /**
170
     * @return $this
171
     */
172
    public function clickSubmit()
173
    {
174
        $this->click('//*[@id="submit-cms"]');
175
176
        return $this;
177
    }
178
179
    /**
180
     * @return $this
181
     */
182
    public function clickPublishButton()
183
    {
184
        $this->click('//*[@id="page-wrapper"]/div[2]/div[2]/div/form/button');
185
186
        return $this;
187
    }
188
189
    /**
190
     * @return $this
191
     */
192
    public function includeJquery()
193
    {
194
        $this->executeJS(
195
            '
196
             var jq = document.createElement("script");
197
             jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
198
             document.getElementsByTagName("head")[0].appendChild(jq);
199
            ',
200
        );
201
202
        $this->wait(3);
203
204
        return $this;
205
    }
206
207
    /**
208
     * @return int
209
     */
210
    public function grabCmsPageId(): int
211
    {
212
        return (int)$this->grabFromCurrentUrl('/id-cms-page=(\d+)/');
213
    }
214
215
    /**
216
     * @return array
217
     */
218
    protected function getLocalizedFakeData(): array
219
    {
220
        if (!$this->localizedFakeData) {
221
            $this->localizedFakeData = [];
222
            $locales = ['de' => 'de_DE', 'en' => 'en_US'];
223
            foreach ($locales as $country => $locale) {
224
                $faker = Factory::create($locale);
225
                $this->localizedFakeData[$country] = [
226
                    'name' => $faker->name,
227
                    'url' => sprintf('/%s/%s', $country, $faker->slug),
228
                ];
229
            }
230
        }
231
232
        return $this->localizedFakeData;
233
    }
234
235
    /**
236
     * @param string $locale
237
     *
238
     * @return string
239
     */
240
    public function getLocalizedName(string $locale): string
241
    {
242
        $localizedFakeData = $this->getLocalizedFakeData();
243
244
        return $localizedFakeData[$locale]['name'];
245
    }
246
247
    /**
248
     * @param string $locale
249
     *
250
     * @return string
251
     */
252
    public function getLocalizedUrl(string $locale): string
253
    {
254
        $localizedFakeData = $this->getLocalizedFakeData();
255
256
        return $localizedFakeData[$locale]['url'];
257
    }
258
}
259