FixtureContext   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 251
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 22
eloc 84
c 4
b 0
f 0
dl 0
loc 251
rs 10

16 Methods

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