Completed
Push — 6.1-ezp25460-draft_preview_sho... ( a8e478 )
by
unknown
96:54 queued 72:33
created

ContentPreviewContext::iModifyAFieldFromTheDraft()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * @license For full copyright and license information view LICENSE file distributed with this source code.
4
 */
5
namespace eZ\Bundle\EzPublishCoreBundle\Features\Context;
6
7
use Behat\Behat\Context\Context;
8
use Behat\Behat\Context\SnippetAcceptingContext;
9
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
10
use eZ\Publish\API\Repository\Values\Content\Content;
11
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
12
use EzSystems\BehatBundle\Context\Browser\Context as BrowserContext;
13
use PHPUnit_Framework_Assert as Assertion;
14
15
class ContentPreviewContext extends BrowserContext implements Context, SnippetAcceptingContext
16
{
17
    /** @var \eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentContext */
18
    private $contentContext;
19
20
    /** @BeforeScenario */
21
    public function gatherContexts(BeforeScenarioScope $scope)
22
    {
23
        $environment = $scope->getEnvironment();
24
25
        $this->contentContext = $environment->getContext('eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentContext');
26
    }
27
28
    /**
29
     * @Given /^I create a draft for a content type that uses a custom location controller$/
30
     */
31
    public function iCreateDraftOfContentTypeWithCustomLocationController()
32
    {
33
        $this->contentContext->createDraft(
34
            'blog_post',
35
            [
36
                'title' => 'Preview draft ' . date('c'),
37
                'body' => '<?xml version="1.0" encoding="UTF-8"?><section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0"><para>This is a paragraph.</para></section>',
38
            ]
39
        );
40
    }
41
42
    /**
43
     * @When /^I preview this draft$/
44
     */
45
    public function iPreviewThisDraft()
46
    {
47
        $this->visit($this->mapToVersionViewUri($this->contentContext->getCurrentDraft()->versionInfo));
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    private function mapToVersionViewUri(VersionInfo $version)
54
    {
55
        return sprintf(
56
            '/content/versionview/%s/%s/%s',
57
            $version->contentInfo->id,
58
            $version->versionNo,
59
            $version->initialLanguageCode
60
        );
61
    }
62
63
    /**
64
     * @Then /^the output is valid$/
65
     */
66
    public function theOutputIsValid()
67
    {
68
        $this->checkForExceptions();
69
    }
70
71
    protected function checkForExceptions()
72
    {
73
        $exceptionElements = $this->getXpath()->findXpath("//div[@class='text-exception']/h1");
74
        $exceptionStackTraceItems = $this->getXpath()->findXpath("//ol[@id='traces-0']/li");
75
        if (count($exceptionElements) > 0) {
76
            $exceptionElement = $exceptionElements[0];
77
            $exceptionLines = [$exceptionElement->getText(), ''];
78
79
            foreach ($exceptionStackTraceItems as $stackTraceItem) {
80
                $html = $stackTraceItem->getHtml();
81
                $html = substr($html, 0, strpos($html, '<a href', 1));
82
                $html = htmlspecialchars_decode(strip_tags($html));
83
                $html = preg_replace('/\s+/', ' ', $html);
84
                $html = str_replace('  (', '(', $html);
85
                $html = str_replace(' ->', '->', $html);
86
                $exceptionLines[] = trim($html);
87
            }
88
            $message = 'An exception occured during rendering:' . implode("\n", $exceptionLines);
89
            Assertion::assertTrue(false, $message);
90
        }
91
    }
92
93
    /**
94
     * @Then /^I see a preview of this draft$/
95
     */
96
    public function iSeeAPreviewOfTheCurrentDraft()
97
    {
98
        $this->assertSession()->elementContains(
99
            'xpath',
100
            "//span[@class='ezstring-field']",
101
            $this->contentContext->getCurrentDraft()->getFieldValue('name')->text
102
        );
103
    }
104
105
    /**
106
     * This could belong in the content context.
107
     *
108
     * @Given /^I modify a field from the draft$/
109
     */
110
    public function iModifyAFieldFromTheDraft()
111
    {
112
        $this->contentContext->updateDraft(
113
            ['name' => 'MODIFIED - ' . $this->contentContext->getCurrentDraft()->getFieldValue('name')->text]
114
        );
115
    }
116
}
117