Passed
Push — master ( 73a69d...ef1b8b )
by Anton
38:47 queued 14s
created

CmsGuiPresentationTester::tryToSeeElement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
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 8
rs 10
cc 2
nc 2
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;
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
     * @param int $formIndex
112
     * @param string $name
113
     * @param string $url
114
     *
115
     * @return $this
116
     */
117
    public function fillLocalizedUrlForm(int $formIndex, string $name, string $url)
118
    {
119
        $nameFieldIdentifier = sprintf('//*[@id="cms_page_pageAttributes_%s_name"]', $formIndex);
120
121
        if (!$this->tryToSeeElement($nameFieldIdentifier)) {
122
            $this->openIboxForElement($nameFieldIdentifier);
123
        }
124
125
        $this->waitForElementVisible($nameFieldIdentifier);
126
127
        $this->fillField($nameFieldIdentifier, $name);
128
129
        $urlFieldIdentifier = sprintf('//*[@id="cms_page_pageAttributes_%s_url"]', $formIndex);
130
131
        if (!$this->tryToSeeElement($urlFieldIdentifier)) {
132
            $this->openIboxForElement($urlFieldIdentifier);
133
        }
134
135
        $this->waitForElementVisible($urlFieldIdentifier);
136
        $this->fillField($urlFieldIdentifier, $url);
137
138
        return $this;
139
    }
140
141
    /**
142
     * @param int $placeHolderIndex
143
     * @param int $localeIndex
144
     * @param string $contents
145
     *
146
     * @return void
147
     */
148
    public function fillPlaceholderContents(int $placeHolderIndex, int $localeIndex, string $contents): void
149
    {
150
        $translationElementId = 'cms_glossary_glossaryAttributes_' . $placeHolderIndex . '_translations_' . $localeIndex . '_translation';
151
152
        $this->executeJS("$('#$translationElementId').text('$contents');");
153
    }
154
155
    /**
156
     * @return $this
157
     */
158
    public function clickSubmit()
159
    {
160
        $this->click('//*[@id="submit-cms"]');
161
162
        return $this;
163
    }
164
165
    /**
166
     * @return $this
167
     */
168
    public function clickPublishButton()
169
    {
170
        $this->click('//*[@id="page-wrapper"]/div[2]/div[2]/div/form/button');
171
172
        return $this;
173
    }
174
175
    /**
176
     * @return $this
177
     */
178
    public function includeJquery()
179
    {
180
        $this->executeJS(
181
            '
182
             var jq = document.createElement("script");
183
             jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
184
             document.getElementsByTagName("head")[0].appendChild(jq);
185
            ',
186
        );
187
188
        $this->wait(3);
189
190
        return $this;
191
    }
192
193
    /**
194
     * @return int
195
     */
196
    public function grabCmsPageId(): int
197
    {
198
        return (int)$this->grabFromCurrentUrl('/id-cms-page=(\d+)/');
199
    }
200
201
    /**
202
     * @return array
203
     */
204
    protected function getLocalizedFakeData(): array
205
    {
206
        if (!$this->localizedFakeData) {
207
            $this->localizedFakeData = [];
208
            $locales = ['de' => 'de_DE', 'en' => 'en_US'];
209
            foreach ($locales as $country => $locale) {
210
                $faker = Factory::create($locale);
211
                $this->localizedFakeData[$country] = [
212
                    'name' => $faker->name,
213
                    'url' => sprintf('/%s/%s', $country, $faker->slug),
214
                ];
215
            }
216
        }
217
218
        return $this->localizedFakeData;
219
    }
220
221
    /**
222
     * @param string $locale
223
     *
224
     * @return string
225
     */
226
    public function getLocalizedName(string $locale): string
227
    {
228
        $localizedFakeData = $this->getLocalizedFakeData();
229
230
        return $localizedFakeData[$locale]['name'];
231
    }
232
233
    /**
234
     * @param string $locale
235
     *
236
     * @return string
237
     */
238
    public function getLocalizedUrl(string $locale): string
239
    {
240
        $localizedFakeData = $this->getLocalizedFakeData();
241
242
        return $localizedFakeData[$locale]['url'];
243
    }
244
}
245