Passed
Pull Request — master (#383)
by
unknown
03:09
created

FeatureContext::iShouldSeeAsTheTitleForBlock()   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 2
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
class FeatureContext extends SilverStripeContext
11
{
12
    /**
13
     * @Then /^I should( not |\s*)see the edit form for block (\d+)$/i
14
     */
15
    public function iShouldSeeTheEditFormForBlock($negative, $position)
16
    {
17
        $iShouldNotSee = $negative === ' not ';
18
19
        $block = $this->getSpecificBlock($position);
20
21
        $form = $block->find('css', '.element-editor-editform');
22
23
        if ($iShouldNotSee) {
24
            assert(!$form || !$form->isVisible(), 'I see the form! Try again later.');
25
        } else {
26
            assertNotNull($form, 'Edit form not found');
27
            assert($form->isVisible());
28
        }
29
    }
30
31
    /**
32
     * @Then /^I (?:should\s)?see a list of blocks$/i
33
     */
34
    public function iShouldSeeAListOfBlocks()
35
    {
36
        assertNotEmpty($this->getBlocks());
37
    }
38
39
    /**
40
     * @Then I should see block :position
41
     */
42
    public function iShouldSeeBlock($position)
43
    {
44
        assertNotNull($this->getSpecificBlock($position));
45
    }
46
47
    /**
48
     * @When /^I click on block (\d+)(?:\sagain)?$/i
49
     */
50
    public function iClickOnBlock($position)
51
    {
52
        $block = $this->getSpecificBlock($position);
53
        assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
54
        $block->click();
55
    }
56
57
    /**
58
     * @When /^I click on the caret button for block (\d+)(?:\sagain)?$/i
59
     */
60
    public function iClickOnTheCaretButtonForBlock($position)
61
    {
62
        $block = $this->getSpecificBlock($position);
63
        $button = $this->getCaretButton($block);
64
        assertNotNull($button, 'Caret button for block ' . $position . ' was not found in the page.');
65
        $button->click();
66
    }
67
68
69
70
    /**
71
     * @Then I should see :text as the title for block :position
72
     */
73
    public function iShouldSeeAsTheTitleForBlock($text, $position)
74
    {
75
        $block = $this->getSpecificBlock($position);
76
        $title = $block->find('css', '.element-editor-header__title');
77
        assertEquals($title->getText(), $text);
78
    }
79
80
    /**
81
     * @Then I should see :text as the summary for block :position
82
     */
83
    public function iShouldSeeAsTheSummaryForBlock($text, $position)
84
    {
85
        $block = $this->getSpecificBlock($position);
86
        $summary = $block->find('css', '.element-editor-summary__content');
87
        assertEquals($summary->getText(), $text);
88
    }
89
90
    /**
91
     * @Then I should see the archive button for block :position
92
     *
93
     * @param int $position
94
     */
95
    public function iShouldSeeArchiveButtonForBlock($position)
96
    {
97
        $this->getArchiveButton($position);
98
    }
99
100
    /**
101
     * @When I hover over block :position
102
     *
103
     * @param int $position
104
     */
105
    public function iHoverOverBlock($position)
106
    {
107
        $block = $this->getSpecificBlock($position);
108
        assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
109
        $block->mouseOver();
110
    }
111
112
    /**
113
     * @When I hover over the icon of block :position
114
     *
115
     * @param int $position
116
     */
117
    public function iHoverOverTheIconOfBlock($position)
118
    {
119
        $block = $this->getSpecificBlock($position);
120
        assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
121
        $icon = $block->find(
122
            'css',
123
            '.element-editor-header .element-editor-header__info .element-editor-header__icon-container'
124
        );
125
        $icon->mouseOver();
126
    }
127
128
    /**
129
     * Returns the blocks from the element editor
130
     *
131
     * @param string $modifier Optional CSS selector modifier
132
     * @return NodeElement[]
133
     */
134
    protected function getBlocks($modifier = '')
135
    {
136
        // Wait for the list to be visible
137
        $this->getSession()->wait(3000, 'window.jQuery(".element-editor .elemental-editor__list").length > 0');
138
139
        // Wait for blocks to be rendered
140
        $this->getSession()->wait(3000, 'window.jQuery(".element-editor__element").length > 0');
141
142
        return $this->getSession()
143
            ->getPage()
144
            ->findAll('css', '.element-editor__element' . $modifier);
145
    }
146
    /**
147
     * Returns the selected element
148
     *
149
     * @param int $position
150
     * @return NodeElement
151
     */
152
    protected function getSpecificBlock($position)
153
    {
154
        $blocks = $this->getBlocks();
155
        /** @var NodeElement $block */
156
        if ($blocks[$position - 1] !== false) {
0 ignored issues
show
introduced by
The condition $blocks[$position - 1] !== false is always true.
Loading history...
157
            return $blocks[$position - 1];
158
        }
159
    }
160
161
    /**
162
     * Returns the archive button for a specific block
163
     *
164
     * @param $position
165
     * @return NodeElement
166
     */
167
    protected function getArchiveButton($position)
168
    {
169
        $block = $this->getSpecificBlock($position);
170
        assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
171
172
        $button = $block->find('css', '.element-editor__actions-archive');
173
        assertNotNull($button, 'Archive button not found');
174
175
        return $button;
176
    }
177
178
    /**
179
     * Returns the caret button for a specific block
180
     *
181
     * @param $block
182
     * @return NodeElement
183
     */
184
    protected function getCaretButton($block)
185
    {
186
        $button = $block->find('css', '.element-editor-header__expand');
187
        assertNotNull($button, 'Caret button not found');
188
189
        return $button;
190
    }
191
}
192