Completed
Push — master ( 82b64b...b48465 )
by
unknown
12s
created

FeatureContext::findPublishButton()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
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
     * Returns the blocks from the element editor
176
     *
177
     * @param string $modifier Optional CSS selector modifier
178
     * @return NodeElement[]
179
     */
180
    protected function getBlocks($modifier = '')
181
    {
182
        // Wait for the list to be visible
183
        $this->getSession()->wait(3000, 'window.jQuery(".element-editor .elemental-editor__list").length > 0');
184
185
        // Wait for blocks to be rendered
186
        $this->getSession()->wait(3000, 'window.jQuery(".element-editor__element").length > 0');
187
188
        return $this->getSession()
189
            ->getPage()
190
            ->findAll('css', '.element-editor__element' . $modifier);
191
    }
192
    /**
193
     * Returns the selected element
194
     *
195
     * @param int $position
196
     * @return NodeElement
197
     */
198
    protected function getSpecificBlock($position)
199
    {
200
        $blocks = $this->getBlocks();
201
        /** @var NodeElement $block */
202
        if ($blocks[$position - 1] !== false) {
0 ignored issues
show
introduced by
The condition $blocks[$position - 1] !== false is always true.
Loading history...
203
            return $blocks[$position - 1];
204
        }
205
    }
206
207
    /**
208
     * Returns the archive button for a specific block
209
     *
210
     * @param $position
211
     * @return NodeElement
212
     */
213
    protected function getArchiveButton($position)
214
    {
215
        $block = $this->getSpecificBlock($position);
216
        assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
217
218
        $button = $block->find('css', '.element-editor__actions-archive');
219
        assertNotNull($button, 'Archive button not found');
220
221
        return $button;
222
    }
223
224
    /**
225
     * Returns the publish button for a specific block if it exists
226
     *
227
     * @param $position
228
     * @return NodeElement|null
229
     */
230
    protected function findPublishButton($position)
231
    {
232
        $block = $this->getSpecificBlock($position);
233
        assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
234
235
        $button = $block->find('css', '.element-editor__actions-publish');
236
237
        return $button;
238
    }
239
240
    /**
241
     * Returns the unpublish button for a specific block if it exists
242
     *
243
     * @param $position
244
     * @return NodeElement|null
245
     */
246
    protected function findUnpublishButton($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-unpublish');
252
253
        return $button;
254
    }
255
256
    /**
257
     * Returns the caret button for a specific block
258
     *
259
     * @param NodeElement $block
260
     * @return NodeElement
261
     */
262
    protected function getCaretButton($block)
263
    {
264
        $button = $block->find('css', '.element-editor-header__expand');
265
        assertNotNull($button, 'Caret button not found');
266
267
        return $button;
268
    }
269
}
270