Completed
Push — master ( 1f89cf...8ee2fd )
by Sergii
02:48
created

UserContext::afterUserScenario()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
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
     * @example
14
     * Then I am logged in as a user with "CRM Client" role and filled fields
15
     *   | Full name                | Sergii Bondarenko |
16
     *   | Position                 | Developer         |
17
     *   | field_crm_user_company   | FFW               |
18
     *
19
     * @param string $roles
20
     *   User roles, separated by comma.
21
     * @param TableNode $fields
22
     *   | Field machine name or label | Value |
23
     *
24
     * @throws \EntityMetadataWrapperException
25
     *   When user object cannot be saved.
26
     * @throws \Exception
27
     *   When required fields are not filled.
28
     *
29
     * @Given /^(?:|I am )logged in as a user with "([^"]*)" role(?:|s)(?:| and filled fields:)$/
30
     */
31
    public function loginCreatedUser($roles, TableNode $fields = null)
32
    {
33
        $this->createDrupalUser($roles, $fields);
34
        $this->loginUser();
35
    }
36
37
    /**
38
     * @see loginCreatedUser()
39
     *
40
     * @Then /^(?:|I )create a user with "([^"]*)" role(?:|s)(?:| and filled fields:)$/
41
     */
42
    public function createDrupalUser($roles, TableNode $fields = null)
43
    {
44
        $this->createUserWithRoles($roles, null !== $fields ? $fields->getRowsHash() : []);
45
    }
46
47
    /**
48
     * @param TableNode $credentials
49
     *   | username | BR0kEN |
50
     *   | password | p4sswd |
51
     *
52
     * @throws \Exception
53
     *   When user cannot be authorized.
54
     *
55
     * @Given /^(?:|I )am logged in with credentials:/
56
     */
57
    public function loginWithCredentials(TableNode $credentials)
58
    {
59
        $this->fillLoginForm($credentials->getRowsHash());
60
    }
61
62
    /**
63
     * This step must be used instead of "I am an anonymous user" because it
64
     * has more strict checking for authenticated user.
65
     *
66
     * @Given /^I am unauthorized user$/
67
     * @Given /^I am log out$/
68
     */
69
    public function logoutDrupalUser()
70
    {
71
        $this->logoutUser();
72
    }
73
74
    /**
75
     * @AfterScenario
76
     */
77
    public function afterUserScenario() {
78
        // Logout, when scenario finished an execution, is required for "Scenario Outline" because an
79
        // object will not be instantiated for every iteration and user data, from previous one, will
80
        // be kept.
81
        $this->logoutUser();
82
    }
83
}
84