Passed
Pull Request — 4 (#980)
by Guy
08:21 queued 05:21
created

pageTemplateIncludesElementalBlocks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
nc 1
nop 0
dl 0
loc 34
rs 9.488
c 0
b 0
f 0
1
<?php
2
namespace DNADesign\Elemental\Tests\Behat\Context;
3
4
use DNADesign\Elemental\Models\ElementContent;
5
use SilverStripe\BehatExtension\Context\FixtureContext as BaseFixtureContext;
6
7
if (!class_exists(BaseFixtureContext::class)) {
8
    return;
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 "([^"]+)" content element with "([^"]+)" content/
17
     *
18
     * @param string $pageTitle
19
     * @param string $type
20
     * @param string $elementTitle
21
     * @param string $elementContent
22
     */
23
    public function theHasAContentElementWithContent($type, $pageTitle, $elementTitle, $elementContent)
24
    {
25
        // Create the page (ElementalArea is created on write and attached to it)
26
        $targetClass = $this->convertTypeToClass($type);
27
28
        $page = $this->getFixtureFactory()->get($targetClass, $pageTitle);
29
        if (!$page) {
0 ignored issues
show
introduced by
$page is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
30
            $page = $this->getFixtureFactory()->createObject($targetClass, $pageTitle);
31
        }
32
33
        $elementalArea = $page->ElementalArea();
34
        $elementalArea->Elements()->add(
35
            $this->getFixtureFactory()->createObject(ElementContent::class, $elementTitle)
36
        );
37
38
        // Create element
39
        $element = $this->getFixtureFactory()->get(ElementContent::class, $elementTitle);
40
41
        if ($element) {
0 ignored issues
show
introduced by
$element is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
42
            $element->HTML = $elementContent;
43
            $element->write();
44
        } else {
45
            $element = $this->getFixtureFactory()->createObject(ElementContent::class, $elementTitle, [
46
                'Title' => $elementTitle,
47
                'HTML' => $elementContent
48
            ]);
49
            $element->write();
50
        }
51
    }
52
53
    /**
54
     * @Given content blocks are not in-line editable
55
     *
56
     * @param $elementTitle
57
     */
58
    public function contentBlocksAreNotInLineEditable()
59
    {
60
        $contentBlockClass = ElementContent::class;
61
        $config = <<<YAML
62
---
63
Name: testonly-content-blocks-not-inline-editable
64
---
65
$contentBlockClass:
66
  inline_editable: false
67
YAML;
68
69
        $file = 'content-blocks-not-inline-editable.yml';
70
        $path = $this->getDestinationConfigFolder($file);
71
        file_put_contents($path ?? '', $config);
72
73
        $this->activatedConfigFiles[] = $path;
74
75
        $this->getMainContext()->visit('/?flush');
76
    }
77
78
    /**
79
     * @Given the page template includes elemental blocks
80
     */
81
    public function pageTemplateIncludesElementalBlocks()
82
    {
83
        $template = <<<'TEMPL'
84
<h1>$Title</h1>
85
<div class="content">
86
    $Content
87
    $ElementalArea
88
</div>
89
TEMPL;
90
91
        $templFile = 'Layout/Page.ss';
92
        $templPath = $this->getDestinationTemplatePath($templFile, true);
0 ignored issues
show
Bug introduced by
The method getDestinationTemplatePath() does not exist on DNADesign\Elemental\Test...\Context\FixtureContext. ( Ignorable by Annotation )

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

92
        /** @scrutinizer ignore-call */ 
93
        $templPath = $this->getDestinationTemplatePath($templFile, true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
        file_put_contents($templPath, $template);
94
95
    $themeConfig = <<<'CONF'
96
---
97
Name: mytheme-override
98
Before: mytheme
99
---
100
SilverStripe\View\SSViewer:
101
  themes:
102
    - '$public'
103
    - '$default'
104
    - 'simple'
105
CONF;
106
107
        $confFile = 'theme-override.yml';
108
        $confPath = $this->getDestinationConfigFolder($confFile);
109
        file_put_contents($confPath, $themeConfig);
110
111
        $this->createdTemplatePaths[] = $templPath;
0 ignored issues
show
Bug Best Practice introduced by
The property createdTemplatePaths does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
112
        $this->activatedConfigFiles[] = $confPath;
113
114
        $this->getMainContext()->visit('/?flush');
115
    }
116
}
117