Passed
Pull Request — master (#12)
by Robbie
01:51
created

FixtureContext::getGalleryItem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 2
dl 0
loc 21
rs 9.8666
c 0
b 0
f 0
1
<?php
2
namespace SilverStripe\ElementalBannerBlock\Tests\Behat\Context;
3
4
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...
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\Assets\Image;
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\ElementalBannerBlock\Block\BannerBlock;
9
10
/**
11
 * Context used to create fixtures in the SilverStripe ORM.
12
 */
13
class FixtureContext extends BaseFixtureContext
14
{
15
   /**
16
     * @Given /(?:the|a) "([^"]+)" "([^"]+)" (?:with|has) a "([^"]+)" banner element with "([^"]+)" content/
17
     *
18
     * @Example Given a "page" "Blocks Page" with a "Alice's Block" banner element with "Some content" content
19
     *
20
     * @param string $pageTitle
21
     * @param string $type
22
     * @param string $elementTitle
23
     * @param string $elementContent
24
     * @param string $file
25
     * @param string $elementLink
26
     */
27
    public function aWithABannerElementWithContentAndAFileAndALink($type, $pageTitle, $elementTitle, $elementContent)
28
    {
29
        // Create the page (ElementalArea is created on write and attached to it)
30
        $targetClass = $this->convertTypeToClass($type);
31
32
        $page = $this->getFixtureFactory()->get($targetClass, $pageTitle);
33
        if (!$page) {
34
            $page = $this->getFixtureFactory()->createObject($targetClass, $pageTitle);
35
        }
36
37
        $element = $this->getFixtureFactory()->get(BannerBlock::class, $elementTitle);
38
39
        if (!$element) {
40
            $elementalArea = $page->ElementalArea();
41
            $elementalArea
42
                ->Elements()
43
                ->add($this->getFixtureFactory()->createObject(BannerBlock::class, $elementTitle));
44
            $element = $this->getFixtureFactory()->get(BannerBlock::class, $elementTitle);
45
        }
46
47
        $file = $this->getFixtureFactory()->get(Image::class, 'Uploads/folder1/file1.jpg');
48
49
        $element->Content = $elementContent;
50
        $element->CallToActionLink = '{"PageID":1,"Text":"Link to home page","Description":"","TargetBlank":false}';
51
        $element->FileID = $file->ID;
52
        $element->write();
53
    }
54
55
56
    /**
57
     * @Given content blocks are not in-line editable
58
     *
59
     * @param $elementTitle
60
     */
61
    public function contentBlocksAreNotInLineEditable()
62
    {
63
        $contentBlockClass = BannerBlock::class;
64
        $config = <<<YAML
65
---
66
Name: testonly-content-blocks-not-inline-editable
67
---
68
$contentBlockClass:
69
  inline_editable: false
70
YAML;
71
72
        $file = 'content-blocks-not-inline-editable.yml';
73
        $path = $this->getDestinationConfigFolder($file);
74
        file_put_contents($path, $config);
75
76
        $this->activatedConfigFiles[] = $path;
0 ignored issues
show
Bug Best Practice introduced by
The property activatedConfigFiles does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77
78
        $this->getMainContext()->visit('/?flush');
79
    }
80
81
    //    --------------------- The below is copied from asset admin -------------------------------
82
    /**
83
     * Select a gallery item by type and name
84
     *
85
     * @Given /^I (?:(?:click on)|(?:select)) the (?:file|folder) named "([^"]+)" in the gallery$/
86
     * @param string $name
87
     */
88
    public function stepISelectGalleryItem($name)
89
    {
90
        $item = $this->getGalleryItem($name);
91
        assertNotNull($item, "File named $name could not be found");
92
        $item->click();
93
    }
94
95
    /**
96
     * Helper for finding items in the visible gallery view
97
     *
98
     * @param string $name Title of item
99
     * @param int $timeout
100
     * @return NodeElement
101
     */
102
    protected function getGalleryItem($name, $timeout = 3)
103
    {
104
        /** @var DocumentElement $page */
105
        $page = $this->getMainContext()->getSession()->getPage();
106
        // Find by cell
107
        $cell = $page->find(
108
            'xpath',
109
            "//div[contains(@class, 'gallery-item')]//div[contains(text(), '{$name}')]"
110
        );
111
        if ($cell) {
112
            return $cell;
113
        }
114
        // Find by row
115
        $row = $page->find(
116
            'xpath',
117
            "//tr[contains(@class, 'gallery__table-row')]//div[contains(text(), '{$name}')]"
118
        );
119
        if ($row) {
120
            return $row;
121
        }
122
        return null;
123
    }
124
}
125