Completed
Push — master ( f25de3...c646c3 )
by Robbie
13s queued 11s
created

FeatureContext::getCaretButton()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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