Passed
Pull Request — master (#13)
by
unknown
03:14
created

iShouldSeeAListOfVersionsInDescendingOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 12 and the first side effect is on line 9.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace SilverStripe\VersionedAdmin\Tests\Behat\Context;
4
5
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...
6
use SilverStripe\BehatExtension\Context\SilverStripeContext;
0 ignored issues
show
Bug introduced by
The type SilverStripe\BehatExtens...ext\SilverStripeContext 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
8
if (!class_exists(SilverStripeContext::class)) {
9
    return;
10
}
11
12
class FeatureContext extends SilverStripeContext
13
{
14
    /**
15
     * @Then I should see a list of versions
16
     */
17
    public function iShouldSeeAListOfVersions()
18
    {
19
        $page = $this->getSession()->getPage();
20
        $versionsTable = $page->find('css', '.history-viewer .table');
21
        assertNotNull($versionsTable, 'I should see a list of versions');
22
    }
23
24
    /**
25
     * @Then I should see a list of versions in descending order
26
     */
27
    public function iShouldSeeAListOfVersionsInDescendingOrder()
28
    {
29
        $versions = $this->getVersions();
30
        $this->clickVersion(current($versions));
31
    }
32
33
    /**
34
     * @When I click on the first version
35
     */
36
    public function iClickOnTheFirstVersion()
37
    {
38
        assertNotNull($this->getLatestVersion(), 'I should see a list of versions');
39
        $this->getLatestVersion()->click();
40
        // Wait for the form builder to load
41
        $this->getSession()->wait(3000);
42
    }
43
44
    /**
45
     * @When I click on version :versionNo
46
     */
47
    public function iClickOnVersion($versionNo)
48
    {
49
        $versions = $this->getVersions(' td:first-child');
50
        $desiredVersion = null;
51
        foreach ($versions as $version) {
52
            /** @var NodeElement $version */
53
            if ($version->getText() == $versionNo) {
54
                $desiredVersion = $version;
55
                break;
56
            }
57
        }
58
        assertNotNull($desiredVersion, 'Desired version ' . $versionNo . ' was not found in the page.');
59
        $this->clickVersion($desiredVersion);
0 ignored issues
show
Bug introduced by
It seems like $desiredVersion can also be of type null; however, parameter $version of SilverStripe\VersionedAd...Context::clickVersion() does only seem to accept Behat\Mink\Element\NodeElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        $this->clickVersion(/** @scrutinizer ignore-type */ $desiredVersion);
Loading history...
60
    }
61
62
    /**
63
     * Click on the given version
64
     *
65
     * @param NodeElement $version
66
     */
67
    protected function clickVersion(NodeElement $version)
68
    {
69
        $version->click();
70
71
        // Wait for the form builder to load
72
        $this->getSession()->wait(3000, 'window.jQuery("#Form_versionForm").length > 0');
73
    }
74
75
    /**
76
     * Returns the versions from the history viewer list (table rows)
77
     *
78
     * @param string $modifier Optional CSS selector modifier
79
     * @return NodeElement[]
80
     */
81
    protected function getVersions($modifier = '')
82
    {
83
        $versions = $this
84
            ->getSession()
85
            ->getPage()
86
            ->findAll('css', '.history-viewer .table tbody tr' . $modifier);
87
88
        assertNotEmpty($versions, 'I see a list of versions');
89
        return $versions;
90
    }
91
92
    /**
93
     * Example: I should see the "Live" badge
94
     * Example: I should not see the "Live" badge
95
     *
96
     * @Then /^I should (not |)see the "([\w\s]+)" badge$/
97
     * @param string $negative
98
     * @param string $text
99
     */
100
    public function iShouldSeeTheBadge($negative, $text)
101
    {
102
        if ($negative) {
103
            $this->assertElementNotOnPage('.history-viewer .badge');
104
        } else {
105
            $this->assertElementContains('.history-viewer .badge', $text);
106
        }
107
    }
108
109
    /**
110
     * Example: I should see "ADMIN User" in the author column in version 1
111
     *
112
     * @Then I should see :text in the author column in version :versionNumber
113
     */
114
    public function iShouldSeeInTheAuthorColumn($text, $versionNumber)
115
    {
116
        $version = $this->getSpecificVersion($versionNumber);
117
        $authorColumn = $version->find('css', 'td:nth-of-type(3)');
118
119
        $exists = strpos($authorColumn->getText(), $text) !== false;
120
        assertTrue($exists, 'Author column contains ' . $text);
121
    }
122
123
    /**
124
     * Example: I should see "Saved" in the record column in version 1
125
     *
126
     * @Then I should see :text in the record column in version :versionNumber
127
     */
128
    public function iShouldSeeInTheRecordColumn($text, $versionNumber)
129
    {
130
        $version = $this->getSpecificVersion($versionNumber);
131
        $recordColumn = $version->find('css', 'td:nth-of-type(2)');
132
133
        $exists = strpos($recordColumn->getText(), $text) !== false;
134
        assertTrue($exists, 'Record column contains ' . $text);
135
    }
136
137
    /**
138
     * @Then I should see :text in the version column in version :versionNumber
139
     */
140
    public function iShouldSeeInTheVersionColumn($text, $versionNumber)
141
    {
142
        $version = $this->getSpecificVersion($versionNumber);
143
        $versionColumn = $version->find('css', 'td');
144
145
        $exists = strpos($versionColumn->getText(), $text) !== false;
146
        assertTrue($exists, 'Version column contains ' . $text);
147
    }
148
149
    /**
150
     * Returns the table row that holds information on the most recent version
151
     */
152
    protected function getLatestVersion()
153
    {
154
        $page = $this->getSession()->getPage();
155
        return $page->find('css', '.history-viewer .table tbody tr');
156
    }
157
158
    /**
159
     * Returns the table row that holds information on the selected version.
160
     *
161
     * @param int $versionNumber
162
     */
163
    protected function getSpecificVersion($versionNumber)
164
    {
165
        $versionColumns = $this->getSession()->getPage()->findAll('css', '.history-viewer tbody tr');
166
        foreach ($versionColumns as $version) {
167
            if (strpos($version->getText(), $versionNumber) !== false) {
168
                return $version;
169
            }
170
        }
171
    }
172
}
173