WysiwygContext::fillInMultipleEditors()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Context\Wysiwyg;
6
7
// Helpers.
8
use Behat\Gherkin\Node\TableNode;
9
10
class WysiwygContext extends RawWysiwygContext
11
{
12
    /**
13
     * @param string $selector
14
     *
15
     * @Given /^(?:|I )work with "([^"]*)" WYSIWYG editor$/
16
     */
17
    public function workWithEditor($selector)
18
    {
19
        $this->getEditor()->setSelector($selector);
20
    }
21
22
    /**
23
     * @param string $text
24
     * @param string $selector
25
     *
26
     * @throws \Exception
27
     *   When editor was not found.
28
     *
29
     * @Given /^(?:|I )fill "([^"]*)" in (?:|"([^"]*)" )WYSIWYG editor$/
30
     */
31
    public function fill($text, $selector = '')
32
    {
33
        $this->getEditor()->fill($text, $selector);
34
    }
35
36
    /**
37
     * @param string $text
38
     * @param string $selector
39
     *
40
     * @throws \Exception
41
     *   When editor was not found.
42
     *
43
     * @When /^(?:|I )type "([^"]*)" in (?:|"([^"]*)" )WYSIWYG editor$/
44
     */
45
    public function type($text, $selector = '')
46
    {
47
        $this->getEditor()->type($text, $selector);
48
    }
49
50
    /**
51
     * @param string $condition
52
     * @param string $text
53
     * @param string $selector
54
     *
55
     * @throws \Exception
56
     *   When editor was not found.
57
     * @throws \RuntimeException
58
     *
59
     * @Then /^(?:|I )should(| not) see "([^"]*)" in (?:|"([^"]*)" )WYSIWYG editor$/
60
     */
61
    public function read($condition, $text, $selector = '')
62
    {
63
        $condition = (bool) $condition;
64
        $wysiwyg = $this->getEditor();
65
        $content = $wysiwyg->read($selector);
66
67
        if (!is_string($content)) {
68
            self::debug(['Returned value:', '%s'], [var_export($content, true)]);
69
70
            throw new \UnexpectedValueException('Could not read WYSIWYG content.');
71
        }
72
73
        self::debug(["Content from WYSIWYG: %s"], [$content]);
74
75
        if (strpos($content, $text) === $condition) {
76
            throw new \RuntimeException(sprintf(
77
                'The text "%s" was %s found in the "%s" WYSIWYG editor.',
78
                $text,
79
                $condition ? '' : 'not',
80
                $wysiwyg->getSelector()
81
            ));
82
        }
83
    }
84
85
    /**
86
     * @param TableNode $fields
87
     *   | Editor locator | Value |
88
     *
89
     * @Then /^(?:|I )fill in following WYSIWYG editors:$/
90
     */
91
    public function fillInMultipleEditors(TableNode $fields)
92
    {
93
        foreach ($fields->getRowsHash() as $selector => $value) {
94
            $this->fill($value, $selector);
95
        }
96
    }
97
98
    /**
99
     * @BeforeScenario @wysiwyg
100
     */
101
    public function beforeScenario()
102
    {
103
        $this->setEditor(self::getTag('wysiwyg', 'CKEditor'), [$this]);
104
    }
105
106
    /**
107
     * @AfterScenario @wysiwyg
108
     */
109
    public function afterScenario()
110
    {
111
        $this->getEditor()->setSelector('');
112
    }
113
}
114