MessageContext::assertNoErrorMessages()   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
ccs 0
cts 17
cp 0
rs 8.439
cc 5
eloc 13
nc 7
nop 0
crap 30
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Context\Message;
6
7
// Helpers.
8
use Behat\Gherkin\Node\TableNode;
9
use Behat\Mink\Element\NodeElement;
10
11
class MessageContext extends RawMessageContext
12
{
13
    /**
14
     * Check that the page have no error messages and fields - error classes.
15
     *
16
     * @throws \RuntimeException
17
     * @throws \Exception
18
     *
19
     * @Then /^(?:|I )should see no errors(?:| on the page)$/
20
     */
21
    public function assertNoErrorMessages()
22
    {
23
        foreach ($this->getMessagesContainers('error') as $element) {
24
            // Some modules are inserted an empty container for errors before
25
            // they are arise. The "Clientside Validation" - one of them.
26
            $text = trim($element->getText());
27
28
            if ('' !== $text) {
29
                throw new \RuntimeException(sprintf(
30
                    'The page "%s" contains following error messages: "%s".',
31
                    self::$pageUrl,
32
                    $text
33
                ));
34
            }
35
        }
36
37
        /** @var NodeElement $formElement */
38
        foreach ($this->getSession()->getPage()->findAll('css', 'input, select, textarea') as $formElement) {
39
            if ($formElement->hasClass('error')) {
40
                throw new \Exception(sprintf(
41
                    'Element "#%s" has an error class.',
42
                    $formElement->getAttribute('id')
43
                ));
44
            }
45
        }
46
    }
47
48
    /**
49
     * @example
50
     * I should see the message "Hello, user."
51
     * I should see the error message "An error occured."
52
     * I should not see the success message "Everything fine."
53
     * I should not see the error message "!name field is required."
54
     *   | !name | E-mail address  |
55
     *
56
     * @param string $negate
57
     *   Indicates that user should or should not see message on the page.
58
     * @param string $type
59
     *   Message type: error, warning, success. Could be empty.
60
     * @param string $message
61
     *   Message to found. Placeholders allowed.
62
     * @param TableNode|array $args
63
     *   Placeholders conformity.
64
     *
65
     * @Then /^(?:|I )should(| not) see the (.* )message "([^"]*)"$/
66
     */
67
    public function assertMessage($negate, $type, $message, $args = [])
68
    {
69
        $type = trim($type);
70
        $negate = (bool) $negate;
71
        $elements = $this->getMessagesContainers($type);
72
73
        if (empty($elements) && !$negate) {
74
            throw new \UnexpectedValueException(sprintf("No $type messages on the page (%s).", self::$pageUrl));
75
        }
76
77
        if ($args instanceof TableNode) {
78
            $args = $args->getRowsHash();
79
        }
80
81
        $translated = t($message, $args);
82
83
        self::debug([
84
            'Input: %s',
85
            'Translated: %s',
86
        ], [
87
            $message,
88
            $translated,
89
        ]);
90
91
        /** @var NodeElement $element */
92
        foreach ($elements as $element) {
93
            $text = trim($element->getText());
94
            $result = strpos($text, $message) !== false || strpos($text, $translated) !== false;
95
96
            if ($negate ? $result : !$result) {
97
                throw new \RuntimeException(sprintf(
98
                    "The $type message %s found on the page (%s).",
99
                    $negate ? 'not' : '',
100
                    self::$pageUrl
101
                ));
102
            }
103
        }
104
    }
105
106
    /**
107
     * @example
108
     * Then should see the following error messages:
109
     *   | !name field is required.  | !name => E-mail address |
110
     *
111
     * @param string $negate
112
     *   Indicates that user should or should not see message on the page.
113
     * @param string $type
114
     *   Message type: error, warning, success. Could be empty.
115
     * @param TableNode $messages
116
     *   Messages to found. Placeholders allowed.
117
     *
118
     * @Then /^(?:|I )should(| not) see the following (.* )messages:$/
119
     */
120
    public function assertMessages($negate, $type, TableNode $messages)
121
    {
122
        foreach ($messages->getRowsHash() as $message => $placeholders) {
123
            $args = [];
124
125
            foreach ((array) $placeholders as $placeholder) {
126
                // Group values: !name => Text.
127
                $data = array_map('trim', explode('=>', $placeholder, 2));
128
129
                if (count($data) === 2) {
130
                    $args[$data[0]] = $data[1];
131
                }
132
            }
133
134
            $this->assertMessage($negate, $type, $message, $args);
135
        }
136
    }
137
}
138