DrushContext::loginWithOneTimeLink()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
ccs 0
cts 16
cp 0
rs 8.5806
cc 4
eloc 13
nc 4
nop 1
crap 20
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