Completed
Push — master ( b48465...dba977 )
by
unknown
9s
created

stepIPressTheButtonInTheAddBlockPopover()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
namespace DNADesign\Elemental\Tests\Behat\Context;
3
4
use Behat\Mink\Element\NodeElement;
5
use SilverStripe\BehatExtension\Context\SilverStripeContext;
6
7
if (!class_exists(SilverStripeContext::class)) {
8
    return;
9
}
10
11
class FeatureContext extends SilverStripeContext
12
{
13
    /**
14
     * @Then /^I should( not |\s+)see the edit form for block (\d+)$/i
15
     */
16
    public function iShouldSeeTheEditFormForBlock($negative, $position)
17
    {
18
        $iShouldNotSee = $negative === ' not ';
19
20
        $block = $this->getSpecificBlock($position);
21
22
        $form = $block->find('css', '.element-editor-editform');
23
24
        if ($iShouldNotSee) {
25
            assertTrue(!$form || !$form->isVisible(), 'I see the form! Try again later.');
26
        } else {
27
            assertNotNull($form, 'Edit form not found');
28
            assertTrue($form->isVisible());
29
        }
30
    }
31
32
    /**
33
     * @Then /^I (?:should\s)?see a list of blocks$/i
34
     */
35
    public function iShouldSeeAListOfBlocks()
36
    {
37
        assertNotEmpty($this->getBlocks());
38
    }
39
40
    /**
41
     * @Then /^I (?:should\s)?see an empty list of blocks$/i
42
     */
43
    public function iShouldSeeAnEmptyListOfBlocks()
44
    {
45
        assertEmpty($this->getBlocks());
46
    }
47
48
    /**
49
     * @Then I should see block :position
50
     */
51
    public function iShouldSeeBlock($position)
52
    {
53
        assertNotNull($this->getSpecificBlock($position));
54
    }
55
56
    /**
57
     * @When /^I click on block (\d+)(?:\sagain)?$/i
58
     */
59
    public function iClickOnBlock($position)
60
    {
61
        $block = $this->getSpecificBlock($position);
62
        assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
63
        $block->click();
64
    }
65
66
    /**
67
     * @When /^I click on the caret button for block (\d+)(?:\sagain)?$/i
68
     */
69
    public function iClickOnTheCaretButtonForBlock($position)
70
    {
71
        $block = $this->getSpecificBlock($position);
72
        $button = $this->getCaretButton($block);
73
        assertNotNull($button, 'Caret button for block ' . $position . ' was not found in the page.');
74
        $button->click();
75
    }
76
77
    /**
78
     * @Then I should see :text as the title for block :position
79
     */
80
    public function iShouldSeeAsTheTitleForBlock($text, $position)
81
    {
82
        $block = $this->getSpecificBlock($position);
83
        $title = $block->find('css', '.element-editor-header__title');
84
        assertEquals($title->getText(), $text);
85
    }
86
87
    /**
88
     * @Then I should see :text as the summary for block :position
89
     */
90
    public function iShouldSeeAsTheSummaryForBlock($text, $position)
91
    {
92
        $block = $this->getSpecificBlock($position);
93
        $summary = $block->find('css', '.element-editor-summary__content');
94
        assertEquals($summary->getText(), $text);
95
    }
96
97
    /**
98
     * @Then I should see the archive button for block :position
99
     *
100
     * @param int $position
101
     */
102
    public function iShouldSeeArchiveButtonForBlock($position)
103
    {
104
        $this->getArchiveButton($position);
105
    }
106
107
    /**
108
     * @Then /^I should( not |\s+)see the publish button for block (\d+)$/i
109
     *
110
     * @param string $negative
111
     * @param int $position
112
     *
113
     */
114
    public function iShouldSeeThePublishButtonForBlock($negative, $position)
115
    {
116
        $iShouldNotSee = $negative === ' not ';
117
118
        $publishButton = $this->findPublishButton($position);
119
120
        if ($iShouldNotSee) {
121
            assertNull($publishButton, 'Publish button displayed (but shouldn\'t)');
122
        } else {
123
            assertNotNull($publishButton, 'Publish button not displayed (but should be)');
124
        }
125
    }
126
127
    /**
128
     * @Then /^I should( not |\s+)see the unpublish button for block (\d+)$/i
129
     *
130
     * @param string $negative
131
     * @param int $position
132
     */
133
    public function iShouldSeeTheUnpublishButtonForBlock($negative, $position)
134
    {
135
        $iShouldNotSee = $negative === ' not ';
136
137
        $unpublishButton = $this->findUnpublishButton($position);
138
139
        if ($iShouldNotSee) {
140
            assertNull($unpublishButton, 'Unpublish button displayed (but shouldn\'t)');
141
        } else {
142
            assertNotNull($unpublishButton, 'Unpublish button not displayed (but should be)');
143
        }
144
    }
145
146
    /**
147
     * @When I hover over block :position
148
     *
149
     * @param int $position
150
     */
151
    public function iHoverOverBlock($position)
152
    {
153
        $block = $this->getSpecificBlock($position);
154
        assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
155
        $block->mouseOver();
156
    }
157
158
    /**
159
     * @When I hover over the icon of block :position
160
     *
161
     * @param int $position
162
     */
163
    public function iHoverOverTheIconOfBlock($position)
164
    {
165
        $block = $this->getSpecificBlock($position);
166
        assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
167
        $icon = $block->find(
168
            'css',
169
            '.element-editor-header .element-editor-header__info .element-editor-header__icon-container'
170
        );
171
        $icon->mouseOver();
172
    }
173
174
    /**
175
     * @Given /^I press the "([^"]*)" button in the add block popover$/
176
     * @param string $text
177
     */
178
    public function stepIPressTheButtonInTheAddBlockPopover($text)
179
    {
180
        $popover = $this->getSession()->getPage()->find('css', '.element-editor-add-element-content');
181
182
        $blockType = strtolower($text);
183
184
        // Selector preferable not font-icon, but other class shared among all buttons
185
        $button = $popover->find('css', '.font-icon-block-'. $blockType);
186
        assertNotNull($button, "{$text} button not found in Add Block popover");
187
        $button->click();
188
    }
189
190
    /**
191
     * Returns the blocks from the element editor
192
     *
193
     * @param string $modifier Optional CSS selector modifier
194
     * @return NodeElement[]
195
     */
196
    protected function getBlocks($modifier = '')
197
    {
198
        // Wait for the list to be visible
199
        $this->getSession()->wait(3000, 'window.jQuery(".element-editor .elemental-editor__list").length > 0');
200
201
        // Wait for blocks to be rendered
202
        $this->getSession()->wait(3000, 'window.jQuery(".element-editor__element").length > 0');
203
204
        return $this->getSession()
205
            ->getPage()
206
            ->findAll('css', '.element-editor__element' . $modifier);
207
    }
208
    /**
209
     * Returns the selected element
210
     *
211
     * @param int $position
212
     * @return NodeElement
213
     */
214
    protected function getSpecificBlock($position)
215
    {
216
        $blocks = $this->getBlocks();
217
        /** @var NodeElement $block */
218
        if ($blocks[$position - 1] !== false) {
0 ignored issues
show
introduced by Raissa North
The condition $blocks[$position - 1] !== false is always true.
Loading history...
219
            return $blocks[$position - 1];
220
        }
221
    }
222
223
    /**
224
     * Returns the archive button for a specific block
225
     *
226
     * @param $position
227
     * @return NodeElement
228
     */
229
    protected function getArchiveButton($position)
230
    {
231
        $block = $this->getSpecificBlock($position);
232
        assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
233
234
        $button = $block->find('css', '.element-editor__actions-archive');
235
        assertNotNull($button, 'Archive button not found');
236
237
        return $button;
238
    }
239
240
    /**
241
     * Returns the publish button for a specific block if it exists
242
     *
243
     * @param $position
244
     * @return NodeElement|null
245
     */
246
    protected function findPublishButton($position)
247
    {
248
        $block = $this->getSpecificBlock($position);
249
        assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
250
251
        $button = $block->find('css', '.element-editor__actions-publish');
252
253
        return $button;
254
    }
255
256
    /**
257
     * Returns the unpublish button for a specific block if it exists
258
     *
259
     * @param $position
260
     * @return NodeElement|null
261
     */
262
    protected function findUnpublishButton($position)
263
    {
264
        $block = $this->getSpecificBlock($position);
265
        assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
266
267
        $button = $block->find('css', '.element-editor__actions-unpublish');
268
269
        return $button;
270
    }
271
272
    /**
273
     * Returns the caret button for a specific block
274
     *
275
     * @param NodeElement $block
276
     * @return NodeElement
277
     */
278
    protected function getCaretButton($block)
279
    {
280
        $button = $block->find('css', '.element-editor-header__expand');
281
        assertNotNull($button, 'Caret button not found');
282
283
        return $button;
284
    }
285
}
286