DrushContext   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 44
ccs 0
cts 16
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B loginWithOneTimeLink() 0 28 4
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Context\Drush;
6
7
class DrushContext extends RawDrushContext
8
{
9
    /**
10
     * @example
11
     * Given I login with one time link[ (1)]
12
     * Then drush uli[ admin]
13
     *
14
     * @param string $argument
15
     *   User ID, email or name. Argument for "drush uli".
16
     *
17
     * @throws \Exception
18
     *
19
     * @Given /^(?:|I )login with one time link(?:| \(([^"]*)\))$/
20
     * @Then /^drush uli(?:| ([^"]*))$/
21
     */
22
    public function loginWithOneTimeLink($argument = '')
23
    {
24
        $userContext = $this->getUserContext();
25
26
        if (empty($argument)) {
27
            $userContext->logoutUser();
28
            $argument = $userContext->createTestUser()->name;
29
        }
30
31
        // Care about not-configured Drupal installations, when
32
        // the "$base_url" variable is not set in "settings.php".
33
        // Also, remove the last underscore symbol from link for
34
        // prevent opening the page for reset the password;
35
        $link = rtrim($this->getOneTimeLoginLink($argument), '_');
36
        $this->visitPath($link);
37
38
        if (
39
            // The "isLoggedIn" method must be called to set authorization cookie for "Goutte"
40
            // session. It must be set to be able to check status codes for the HTTP requests.
41
            !$userContext->isLoggedIn() &&
42
            !preg_match(
43
                sprintf("/%s/i", t('You have just used your one-time login link.')),
44
                $this->getWorkingElement()->getText()
45
            )
46
        ) {
47
            throw new \Exception(sprintf('Cannot login with one time link: "%s"', $link));
48
        }
49
    }
50
}
51