Passed
Push — 1 ( ed6036...82858f )
by Steve
09:32 queued 11s
created

FixtureContext::iClickTheElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace SilverStripe\CampaignAdmin\Tests\Behat\Context;
4
5
use Behat\Mink\Element\DocumentElement;
0 ignored issues
show
Bug introduced by
The type Behat\Mink\Element\DocumentElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Behat\Mink\Element\NodeElement;
0 ignored issues
show
Bug introduced by
The type Behat\Mink\Element\NodeElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use SilverStripe\BehatExtension\Context\FixtureContext as BaseFixtureContext;
0 ignored issues
show
Bug introduced by
The type SilverStripe\BehatExtension\Context\FixtureContext was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use SilverStripe\BehatExtension\Utility\StepHelper;
0 ignored issues
show
Bug introduced by
The type SilverStripe\BehatExtension\Utility\StepHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use SilverStripe\MinkFacebookWebDriver\FacebookWebDriver;
0 ignored issues
show
Bug introduced by
The type SilverStripe\MinkFaceboo...river\FacebookWebDriver was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SilverStripe\Versioned\ChangeSet;
11
12
/**
13
 * Context used to create fixtures in the SilverStripe ORM.
14
 */
15
class FixtureContext extends BaseFixtureContext
16
{
17
    use StepHelper;
18
19
    /**
20
     * @Then /^I should see the campaign "([^"]+)"$/
21
     * @param string $name
22
     */
23
    public function iShouldSeeTheCampaign($name)
24
    {
25
        $item = $this->getCampaign($name);
26
27
        assertNotNull($item, "Could not find campaign \"$name\" in the list");
28
    }
29
30
    /**
31
     * @Then /^I should not see the campaign "([^"]+)"$/
32
     * @param string $name
33
     */
34
    public function iShouldNotSeeTheCampaign($name)
35
    {
36
        $item = $this->getCampaign($name);
37
38
        assertNull($item, "Found campaign \"$name\" in the list");
39
    }
40
41
    /**
42
     * @Then /^I should see "([^"]+)" in column (\d+) of the "([^"]+)" campaign$/
43
     * @param string $text
44
     * @param string $column
45
     * @param string $name
46
     */
47
    public function iShouldSeeInTheCampaignColumn($text, $column, $name)
48
    {
49
        $campaignRow = $this->getCampaign($name);
50
        assertNotNull($campaignRow, sprintf('Could not find campaign "%s"', $name));
51
52
        $cell = $campaignRow->find('xpath', "/td[{$column}][contains(text(), '{$text}')]");
53
54
        assertNotNull($cell, sprintf('Could not find "%s" in column %d of "%s"', $text, $column, $name));
55
    }
56
57
    /**
58
     * @Given /^I view the campaign "([^"]*)"$/
59
     * @param $name
60
     */
61
    public function iViewTheCampaign($name)
62
    {
63
        $item = $this->getCampaign($name);
64
        assertNotNull($item, sprintf('Campaign %s not found', $name));
65
66
        $item->find('css', 'td')->click();
67
    }
68
69
    /**
70
     * @Given /^I edit the campaign "([^"]*)"$/
71
     * @param $name
72
     */
73
    public function iEditTheCampaign($name)
74
    {
75
        $item = $this->getCampaign($name);
76
        assertNotNull($item, sprintf('Campaign %s not found', $name));
77
78
        $button = $item->find('css', '.font-icon-cog');
79
        assertNotNull($button, sprintf('Campaign %s has no edit button', $name));
80
        $button->click();
81
    }
82
83
    /**
84
     * @Given /^I delete the campaign "([^"]*)"$/
85
     * @param $name
86
     */
87
    public function iDeleteTheCampaign($name)
88
    {
89
        $item = $this->getCampaign($name);
90
        assertNotNull($item, sprintf('Campaign %s not found', $name));
91
92
        $button = $item->find('css', '.font-icon-cancel');
93
        assertNotNull($button, sprintf('Campaign %s has no delete button', $name));
94
        $button->click();
95
96
        /** @var FacebookWebDriver $driver */
97
        $driver = $this->getMainContext()->getSession()->getDriver();
98
        $webDriver = $driver->getWebDriver();
99
        $webDriver->switchTo()->alert()->accept();
100
    }
101
102
    /**
103
     * @Then /^I should see the "([^"]*)" form$/
104
     * @param string $id HTML ID of form
105
     */
106
    public function iShouldSeeTheForm($id)
107
    {
108
        /** @var DocumentElement $page */
109
        $page = $this->getMainContext()->getSession()->getPage();
110
        $form = $page->find('css', "form#{$id}");
111
        assertNotNull($form, "form with id $id could not be found");
112
        assertTrue($form->isVisible(), "form with id $id is not visible");
113
    }
114
115
    /**
116
     * Checks that the message box contains specified text.
117
     *
118
     * @Then /^I should see "(?P<text>(?:[^"]|\\")*)" in the message box$/
119
     * @param string $text
120
     */
121
    public function assertMessageBoxContainsText($text)
122
    {
123
        /** @var FeatureContext $mainContext */
124
        $mainContext = $this->getMainContext();
125
        $mainContext
126
            ->assertSession()
127
            ->elementTextContains('css', '.message-box', str_replace('\\"', '"', $text));
128
    }
129
130
    /**
131
     * @Then I should see a modal titled :title
132
     * @param string $title
133
     */
134
    public function iShouldSeeAModalTitled($title)
135
    {
136
        $page = $this->getMainContext()->getSession()->getPage();
137
        $modalTitle = $page->find('css', '[role=dialog] .modal-header > .modal-title');
138
        assertNotNull($modalTitle, 'No modal on the page');
139
        assertTrue($modalTitle->getText() == $title);
140
    }
141
142
    /**
143
     * @Then /^I close the modal$/
144
     */
145
    public function iCloseTheModal()
146
    {
147
        /** @var DocumentElement $page */
148
        $page = $this->getMainContext()->getSession()->getPage();
149
        $button = $page->find('css', '.modal-header .close');
150
        assertNotNull($button, 'Close button not found');
151
152
        $button->click();
153
    }
154
155
    /**
156
     * @Given /^I click the "([^"]+)" gallery item$/
157
     * @param $name
158
     */
159
    public function iClickTheGalleryItem($name)
160
    {
161
        /** @var DocumentElement $page */
162
        $page = $this->getMainContext()->getSession()->getPage();
163
        // Find by cell
164
        $cell = $page->find(
165
            'xpath',
166
            "//div[contains(@class, 'gallery-item')]//div[contains(text(), '{$name}')]"
167
        );
168
169
        assertNotNull($cell, sprintf('Gallery item "%s" could not be found', $name));
170
171
        $cell->click();
172
    }
173
174
    /**
175
     * @Given /^I should( not? |\s*)see the "([^"]+)" campaign item$/
176
     * @param $negate boolean
177
     * @param $name string
178
     */
179
    public function iSeeTheCampaignItem($negate, $name)
180
    {
181
        $item = $this->getCampaignItem($name);
182
        $shouldSee = !trim($negate);
183
184
        if ($shouldSee) {
185
            assertNotNull($item, sprintf('Item "%s" could not be found', $name));
186
            assertTrue($item->isVisible(), sprintf('Item "%s" is not visible', $name));
187
        } elseif ($item) {
0 ignored issues
show
introduced by
$item is of type Behat\Mink\Element\NodeElement, thus it always evaluated to true.
Loading history...
188
            assertFalse($item->isVisible(), sprintf('Item "%s" is visible', $name));
189
        } else {
190
            assertNull($item, sprintf('Item "%s" exists', $name));
191
        }
192
    }
193
194
    /**
195
     * @Given /^I select the "([^"]+)" campaign item$/
196
     * @param $name
197
     */
198
    public function iSelectTheCampaignItem($name)
199
    {
200
        $item = $this->getCampaignItem($name);
201
        assertNotNull($item, sprintf('Item "%s" could not be found', $name));
202
        $item->click();
203
    }
204
205
    /**
206
     * @Given /^a campaign "([^"]+)" with changes (".*)$/
207
     * @param $id
208
     * @param $data
209
     */
210
    public function stepCreateCampaignWithItems($id, $data)
211
    {
212
        $class = $this->convertTypeToClass(ChangeSet::class);
213
        /* @var ChangeSet $campaign */
214
        $campaign = $this->getFixtureFactory()->get($class, $id);
215
        if (!$campaign) {
0 ignored issues
show
introduced by
$campaign is of type SilverStripe\Versioned\ChangeSet, thus it always evaluated to true.
Loading history...
216
            $campaign = $this->getFixtureFactory()->createObject($class, $id);
217
        }
218
219
        preg_match_all(
220
            '/"(?<content_class>[^"]+)"\s*=\s*"(?<identifier>[^"]+)"/',
221
            $data,
222
            $matches
223
        );
224
        $itemMap = array_combine($matches['content_class'], $matches['identifier']);
225
        foreach ($itemMap as $contentClass => $identifier) {
226
            $class = $this->convertTypeToClass($contentClass);
227
            $record = $this->getFixtureFactory()->get($class, $identifier);
228
            if (!$record) {
229
                $record = $this->getFixtureFactory()->createObject($class, $identifier);
230
            }
231
            $campaign->addObject($record);
232
        }
233
    }
234
235
    /**
236
     * Helper for finding items in the visible campaign view
237
     *
238
     * @param string $name Title of item
239
     * @return NodeElement
240
     */
241
    protected function getCampaign($name)
242
    {
243
        /** @var DocumentElement $page */
244
        $page = $this->getMainContext()->getSession()->getPage();
245
        // Find by row
246
        $row = $page->find(
247
            'xpath',
248
            "//tr[contains(@class, 'grid-field__row')]//td[contains(text(), '{$name}')]/.."
249
        );
250
251
        return $row ?: null;
252
    }
253
254
    /**
255
     * Gets a change set item in the detail view
256
     * @param $name
257
     * @return NodeElement
258
     */
259
    protected function getCampaignItem($name)
260
    {
261
        /** @var DocumentElement $page */
262
        $page = $this->getMainContext()->getSession()->getPage();
263
        return $page->find(
264
            'xpath',
265
            "//h4[contains(@class, 'list-group-item__heading') and contains(text(), '{$name}')]"
266
        );
267
    }
268
}
269