Completed
Push — preview_bdd ( 427ffe )
by
unknown
41:40 queued 18:26
created

ContentPreviewContext::iModifyAFieldFromTheDraft()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
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 eZ\Publish\API\Repository\Values\Content\Content;
10
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
11
use EzSystems\BehatBundle\Context\Browser\Context as BrowserContext;
12
use PHPUnit_Framework_Assert as Assertion;
13
14
class ContentPreviewContext extends BrowserContext implements Context, SnippetAcceptingContext
15
{
16
    /** @var Content */
17
    private $currentDraft;
18
19
    /**
20
     * @Given /^I create an article draft$/
21
     */
22
    public function iCreateAnArticleDraft()
23
    {
24
        $draft = $this->createArticleDraft('Preview draft ' . date('c'));
25
        $this->currentDraft = $draft;
26
    }
27
28
    /**
29
     * That would be a blog post.
30
     *
31
     * @Given /^I create a draft for a content type that uses a custom location controller$/
32
     */
33
    public function iCreateDraftContentTypeWithCustomLocationController()
34
    {
35
        $fields = array(
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
        $draft = $this->getBasicContentManager()->createContentDraft(2, 'blog_post', $fields);
40
        $this->currentDraft = $draft;
41
    }
42
43
    /**
44
     * @When /^I preview this draft$/
45
     */
46
    public function iPreviewThisDraft()
47
    {
48
        if (!$this->currentDraft instanceof Content) {
49
            throw new \Exception("'this draft' is not set. Bad context ?");
50
        }
51
        $this->visit($this->mapToVersionViewUri($this->currentDraft->versionInfo));
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    private function mapToVersionViewUri(VersionInfo $version)
58
    {
59
        return sprintf(
60
            '/content/versionview/%s/%s/%s',
61
            $version->contentInfo->id,
62
            $version->versionNo,
63
            $version->initialLanguageCode
64
        );
65
    }
66
67
    /**
68
     * @Then /^the output is valid$/
69
     */
70
    public function theOutputIsValid()
71
    {
72
        $this->checkForExceptions();
73
    }
74
75
    protected function checkForExceptions()
76
    {
77
        $exceptionElements = $this->getXpath()->findXpath("//div[@class='text-exception']/h1");
78
        $exceptionStackTraceItems = $this->getXpath()->findXpath("//ol[@id='traces-0']/li");
79
        if (count($exceptionElements) > 0) {
80
            $exceptionElement = $exceptionElements[0];
81
            $exceptionLines = [$exceptionElement->getText(), ''];
82
83
            foreach ($exceptionStackTraceItems as $stackTraceItem) {
84
                $html = $stackTraceItem->getHtml();
85
                $html = substr($html, 0, strpos($html, '<a href', 1));
86
                $html = htmlspecialchars_decode(strip_tags($html));
87
                $html = preg_replace('/\s+/', ' ', $html);
88
                $html = str_replace('  (', '(', $html);
89
                $html = str_replace(' ->', '->', $html);
90
                $exceptionLines[] = trim($html);
91
            }
92
            $message = 'An exception occured during rendering:' . implode("\n", $exceptionLines);
93
            Assertion::assertTrue(false, $message);
94
        }
95
    }
96
97
    /**
98
     * @Given /^I create a draft of an existing content item$/
99
     */
100
    public function iCreateADraftOfAnExistingContentItem()
101
    {
102
        // the content folder should do, since we won't publish it
103
        $this->currentDraft = $this->getBasicContentManager()->createContentDraftForContent(1);
104
    }
105
106
    /**
107
     * @Given /^I modify a field from the draft$/
108
     */
109
    public function iModifyAFieldFromTheDraft()
110
    {
111
        $this->getBasicContentManager()->updateDraft(
112
            $this->currentDraft,
113
            ['name' => 'MODIFIED - ' . $this->currentDraft->getField('name')->value->text]
114
        );
115
    }
116
117
    /**
118
     * @Then /^I get a preview of this draft$/
119
     */
120
    public function iGetAPreviewOfThisDraft()
121
    {
122
        $this->assertSession()->elementContains(
123
            'xpath',
124
            "//span[@class='ezstring-field']",
125
            $this->currentDraft->getField('name')->value->text
126
        );
127
    }
128
}
129