UserContext::visitPage()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 0
cts 8
cp 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
crap 12
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Context\User;
6
7
// Helpers.
8
use Behat\Gherkin\Node\TableNode;
9
10
class UserContext extends RawUserContext
11
{
12
    /**
13
     * @param string $operation
14
     *   Allowable values: "edit", "view", "visit".
15
     * @param string $value
16
     *   The name or email of a user.
17
     *
18
     * @When /^(?:|I )(visit|view|edit) (?:the "([^"]+)"|current) user$/
19
     */
20
    public function visitPage($operation, $value = '')
21
    {
22
        $column = $value;
23
24
        // If we have an empty value then try to use current user.
25
        if ('' !== $value) {
26
            $column = strpos($value, '@') === false ? 'name' : 'mail';
27
        }
28
29
        // This helps us restrict an access for editing for users without correct permissions.
30
        // Will check for 403 HTTP status code.
31
        $this->getRedirectContext()
32
            // Check user by email if value contains a "dog" symbol.
33
            ->visitPage($this->entityUrl($operation, $column, $value));
34
    }
35
36
    /**
37
     * @example
38
     * Then I am logged in as a user with "CRM Client" role and filled fields
39
     *   | Full name                | Sergii Bondarenko |
40
     *   | Position                 | Developer         |
41
     *   | field_crm_user_company   | FFW               |
42
     *
43
     * @param string $roles
44
     *   User roles, separated by comma.
45
     * @param TableNode $fields
46
     *   | Field machine name or label | Value |
47
     *
48
     * @throws \EntityMetadataWrapperException
49
     *   When user object cannot be saved.
50
     * @throws \Exception
51
     *   When required fields are not filled.
52
     *
53
     * @Given /^(?:|I am )logged in as a user with "([^"]*)" role(?:|s)(?:| and filled fields:)$/
54
     */
55
    public function loginCreatedUser($roles, TableNode $fields = null)
56
    {
57
        $this->createDrupalUser($roles, $fields);
58
        $this->loginUser();
59
    }
60
61
    /**
62
     * @see loginCreatedUser()
63
     *
64
     * @Then /^(?:|I )create a user with "([^"]*)" role(?:|s)(?:| and filled fields:)$/
65
     */
66
    public function createDrupalUser($roles, TableNode $fields = null)
67
    {
68
        $this->createUserWithRoles($roles, null !== $fields ? $fields->getRowsHash() : []);
69
    }
70
71
    /**
72
     * @param TableNode $credentials
73
     *   | username | BR0kEN |
74
     *   | password | p4sswd |
75
     *
76
     * @throws \Exception
77
     *   When user cannot be authorized.
78
     *
79
     * @Given /^(?:|I )am logged in with credentials:/
80
     */
81
    public function loginWithCredentials(TableNode $credentials)
82
    {
83
        $this->fillLoginForm($credentials->getRowsHash());
84
    }
85
86
    /**
87
     * This step must be used instead of "I am an anonymous user" because it
88
     * has more strict checking for authenticated user.
89
     *
90
     * @Given /^I am unauthorized user$/
91
     * @Given /^I am log out$/
92
     */
93
    public function logoutDrupalUser()
94
    {
95
        $this->logoutUser();
96
    }
97
98
    /**
99
     * @AfterScenario
100
     */
101
    public function afterUserScenario()
102
    {
103
        // Logout, when scenario finished an execution, is required for "Scenario Outline" because an
104
        // object will not be instantiated for every iteration and user data, from previous one, will
105
        // be kept.
106
        $this->logoutUser();
107
    }
108
}
109