Passed
Pull Request — master (#404)
by
unknown
02:02
created

FeatureContext::iClickOnBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
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
     * @When I hover over block :position
109
     *
110
     * @param int $position
111
     */
112
    public function iHoverOverBlock($position)
113
    {
114
        $block = $this->getSpecificBlock($position);
115
        assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
116
        $block->mouseOver();
117
    }
118
119
    /**
120
     * @When I hover over the icon of block :position
121
     *
122
     * @param int $position
123
     */
124
    public function iHoverOverTheIconOfBlock($position)
125
    {
126
        $block = $this->getSpecificBlock($position);
127
        assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
128
        $icon = $block->find(
129
            'css',
130
            '.element-editor-header .element-editor-header__info .element-editor-header__icon-container'
131
        );
132
        $icon->mouseOver();
133
    }
134
135
    /**
136
     * @Given /^I press the "([^"]*)" button in the add block popover$/
137
     * @param string $text
138
     */
139
    public function stepIPressTheButtonInTheAddBlockPopover($text)
140
    {
141
        $popover = $this->getSession()->getPage()->find('css', '.element-editor-add-element-content');
142
143
        $blockType = strtolower($text);
144
145
        // Selector preferable not font-icon, but other class shared among all buttons
146
        $button = $popover->find('css', '.font-icon-block-'. $blockType);
147
        assertNotNull($button, "{$text} button not found in Add Block popover");
148
        $button->click();
149
    }
150
151
    /**
152
     * Returns the blocks from the element editor
153
     *
154
     * @param string $modifier Optional CSS selector modifier
155
     * @return NodeElement[]
156
     */
157
    protected function getBlocks($modifier = '')
158
    {
159
        // Wait for the list to be visible
160
        $this->getSession()->wait(3000, 'window.jQuery(".element-editor .elemental-editor__list").length > 0');
161
162
        // Wait for blocks to be rendered
163
        $this->getSession()->wait(3000, 'window.jQuery(".element-editor__element").length > 0');
164
165
        return $this->getSession()
166
            ->getPage()
167
            ->findAll('css', '.element-editor__element' . $modifier);
168
    }
169
    /**
170
     * Returns the selected element
171
     *
172
     * @param int $position
173
     * @return NodeElement
174
     */
175
    protected function getSpecificBlock($position)
176
    {
177
        $blocks = $this->getBlocks();
178
        /** @var NodeElement $block */
179
        if ($blocks[$position - 1] !== false) {
0 ignored issues
show
introduced by
The condition $blocks[$position - 1] !== false is always true.
Loading history...
180
            return $blocks[$position - 1];
181
        }
182
    }
183
184
    /**
185
     * Returns the archive button for a specific block
186
     *
187
     * @param $position
188
     * @return NodeElement
189
     */
190
    protected function getArchiveButton($position)
191
    {
192
        $block = $this->getSpecificBlock($position);
193
        assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
194
195
        $button = $block->find('css', '.element-editor__actions-archive');
196
        assertNotNull($button, 'Archive button not found');
197
198
        return $button;
199
    }
200
201
    /**
202
     * Returns the caret button for a specific block
203
     *
204
     * @param NodeElement $block
205
     * @return NodeElement
206
     */
207
    protected function getCaretButton($block)
208
    {
209
        $button = $block->find('css', '.element-editor-header__expand');
210
        assertNotNull($button, 'Caret button not found');
211
212
        return $button;
213
    }
214
}
215