Completed
Push — test-EZP-26707-issearchable-fu... ( 774d65...d743e2 )
by
unknown
24:59
created

QueryControllerContext::visitMatchedContent()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 34
Code Lines 24

Duplication

Lines 16
Ratio 47.06 %

Importance

Changes 0
Metric Value
cc 4
eloc 24
c 0
b 0
f 0
nc 3
nop 0
dl 16
loc 34
rs 8.5806
1
<?php
2
/**
3
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 */
6
namespace eZ\Bundle\EzPublishCoreBundle\Features\Context;
7
8
use Behat\Behat\Context\Context;
9
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
10
use Behat\Gherkin\Node\PyStringNode;
11
use Behat\Mink\Element\NodeElement;
12
use Behat\MinkExtension\Context\RawMinkContext;
13
use eZ\Publish\API\Repository\Repository;
14
use eZ\Publish\API\Repository\Values\Content\Content;
15
use EzSystems\PlatformBehatBundle\Context\RepositoryContext;
16
use PHPUnit_Framework_Assert;
17
use Symfony\Component\Filesystem\Filesystem;
18
use Symfony\Component\Yaml\Yaml;
19
20
class QueryControllerContext extends RawMinkContext implements Context
21
{
22
    use RepositoryContext;
23
24
    /**
25
     * @var YamlConfigurationContext
26
     */
27
    private $configurationContext;
28
29
    /**
30
     * Content item matched by the view configuration.
31
     * @var Content
32
     */
33
    private $matchedContent;
34
35
    /**
36
     * QueryControllerContext constructor.
37
     * @injectService $repository @ezpublish.api.repository
38
     */
39
    public function __construct(Repository $repository)
40
    {
41
        $this->repository = $repository;
42
    }
43
44
    /** @BeforeScenario */
45
    public function gatherContexts(BeforeScenarioScope $scope)
46
    {
47
        $environment = $scope->getEnvironment();
48
49
        $this->configurationContext = $environment->getContext(
50
            'eZ\Bundle\EzPublishCoreBundle\Features\Context\YamlConfigurationContext'
51
        );
52
    }
53
54
    /**
55
     * @Given /^the following content view configuration block:$/
56
     */
57
    public function addContentViewConfigurationBlock(PyStringNode $string)
58
    {
59
        $configurationBlock = array_merge(
60
            Yaml::parse($string),
61
            [
62
                'template' => 'EzPlatformBehatBundle::dump.html.twig',
63
                'match' => [
64
                    'Id\Content' => $this->matchedContent->id,
65
                ],
66
            ]
67
        );
68
69
        $configurationBlockName = 'behat_query_controller_' . $this->matchedContent->id;
70
71
        $configuration = [
72
            'ezpublish' => [
73
                'system' => [
74
                    'default' => [
75
                        'content_view' => [
76
                            'full' => [
77
                                $configurationBlockName => $configurationBlock,
78
                            ],
79
                        ],
80
                    ],
81
                ],
82
            ],
83
        ];
84
85
        $this->configurationContext->addConfiguration($configuration);
86
    }
87
88
    /**
89
     * @Given /^a content item that matches the view configuration block below$/
90
     */
91
    public function aContentItemThatMatchesTheViewConfigurationBlockBelow()
92
    {
93
        $this->matchedContent = $this->createFolder();
94
    }
95
96
    /**
97
     * @return Content
98
     */
99
    private function createFolder()
100
    {
101
        $repository = $this->getRepository();
102
        $contentService = $repository->getContentService();
103
        $contentTypeService = $repository->getContentTypeService();
104
        $locationService = $repository->getLocationService();
105
106
        $struct = $contentService->newContentCreateStruct(
107
            $contentTypeService->loadContentTypeByIdentifier('folder'),
108
            'eng-GB'
109
        );
110
111
        $struct->setField('name', uniqid('Query Controller BDD '));
112
113
        $contentDraft = $contentService->createContent(
114
            $struct,
115
            [$locationService->newLocationCreateStruct(2)]
116
        );
117
        $contentService->publishVersion($contentDraft->versionInfo);
118
119
        return $contentService->loadContent($contentDraft->id);
120
    }
121
122
    /**
123
     * @Given /^a LocationChildren QueryType defined in "([^"]*)":$/
124
     */
125
    public function createPhpFile($phpFilePath, PyStringNode $phpFileContents)
126
    {
127
        $fs = new Filesystem();
128
        $fs->mkdir(dirname($phpFilePath));
129
        $fs->dumpFile($phpFilePath, $phpFileContents);
130
        shell_exec('php app/console --env=behat cache:clear');
131
    }
132
133
    /**
134
     * @When /^I view a content matched by the view configuration above$/
135
     */
136
    public function visitMatchedContent()
137
    {
138
        $urlAliasService = $this->getRepository()->getURLAliasService();
139
        $urlAlias = $urlAliasService->reverseLookup(
140
            $this->getRepository()->getLocationService()->loadLocation(
141
                $this->matchedContent->contentInfo->mainLocationId
142
            )
143
        );
144
145
        $this->visitPath($urlAlias->path);
146
147
        if ($this->getSession()->getStatusCode() !== 200) {
148
            $page = $this->getSession()->getPage();
149
            $exceptionElements = $page->findAll('xpath', "//div[@class='text-exception']/h1");
150
            $exceptionStackTraceItems = $page->findAll('xpath', "//ol[@id='traces-0']/li");
151 View Code Duplication
            if (count($exceptionElements) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
                $exceptionElement = $exceptionElements[0];
153
                $exceptionLines = [$exceptionElement->getText(), ''];
154
155
                foreach ($exceptionStackTraceItems as $stackTraceItem) {
156
                    $html = $stackTraceItem->getHtml();
157
                    $html = substr($html, 0, strpos($html, '<a href', 1));
158
                    $html = htmlspecialchars_decode(strip_tags($html));
159
                    $html = preg_replace('/\s+/', ' ', $html);
160
                    $html = str_replace('  (', '(', $html);
161
                    $html = str_replace(' ->', '->', $html);
162
                    $exceptionLines[] = trim($html);
163
                }
164
                $message = 'An exception occurred during rendering:' . implode("\n", $exceptionLines);
165
                PHPUnit_Framework_Assert::assertTrue(false, $message);
166
            }
167
        }
168
        $this->assertSession()->statusCodeEquals(200);
169
    }
170
171
    /**
172
     * @Then /^the viewed content's main location id is mapped to the parentLocationId QueryType parameter$/
173
     */
174
    public function theViewedContentSMainLocationIdIsMappedToTheParentLocationIdQueryTypeParameter()
175
    {
176
        // not sure how to assert that
177
    }
178
179
    /**
180
     * @Then /^a LocationChildren Query is built from the LocationChildren QueryType$/
181
     */
182
    public function aLocationChildrenQueryIsBuiltFromTheLocationChildrenQueryType()
183
    {
184
        // not sure how to assert that either
185
    }
186
187
    /**
188
     * @Given /^a Location Search is executed with the LocationChildren Query$/
189
     */
190
    public function aLocationSearchIsExecutedWithTheLocationChildrenQuery()
191
    {
192
        // still not sure...
193
    }
194
195
    /**
196
     * @Given /^the Query results are assigned to the "([^"]*)" twig variable$/
197
     */
198
    public function theQueryResultsAreAssignedToTheTwigVariable($twigVariableName)
199
    {
200
        $variableFound = false;
201
202
        $page = $this->getSession()->getPage();
203
        $variableNodes = $page->findAll('css', 'pre.sf-dump > samp > span.sf-dump-key');
204
205
        /** @var NodeElement $variableNode */
206
        foreach ($variableNodes as $variableNode) {
207
            if ($variableNode->getText() === $twigVariableName) {
208
                $variableFound = true;
209
            }
210
        }
211
212
        PHPUnit_Framework_Assert::assertTrue(
213
            $variableFound,
214
            "The $twigVariableName twig variable was not set"
215
        );
216
    }
217
}
218