Passed
Push — master ( 679eaf...aeba21 )
by Ross
41:47
created

AbstractTestClass   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 88
rs 10
c 1
b 1
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormKey() 0 8 1
A login() 0 12 2
A loginAdmin() 0 20 2
A createObject() 0 8 2
A assertRedirectsToHomePage() 0 4 1
A getAdminSession() 0 9 2
A disableSecretKeys() 0 4 1
1
<?php
2
/**
3
 * A two factor authentication module that protects both the admin and customer logins
4
 * Copyright (C) 2017  Ross Mitchell
5
 *
6
 * This file is part of Rossmitchell/Twofactor.
7
 *
8
 * Rossmitchell/Twofactor is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
namespace Rossmitchell\Twofactor\Tests\Integration\Abstracts;
23
24
use Magento\Backend\Model\Auth;
25
use Magento\Backend\Model\UrlInterface;
26
use Magento\Customer\Model\Customer;
27
use Magento\Customer\Model\Session;
28
use Magento\Framework\Data\Form\FormKey;
29
use Magento\Security\Model\Plugin\Auth as AuthPlugin;
30
use Magento\TestFramework\TestCase\AbstractController;
31
use Magento\Backend\Model\Auth\Session\Proxy;
32
use Magento\User\Model\User;
33
34
class AbstractTestClass extends AbstractController
35
{
36
37
    public function getFormKey()
38
    {
39
        $formKeyClass = $this->createObject(FormKey::class, false);
40
        $formKey      = $formKeyClass->getFormKey();
41
        $formKeyClass->set($formKey);
42
43
        return $formKey;
44
    }
45
46
    /**
47
     * Login the user
48
     *
49
     * @param string $customerEmail Customer to mark as logged in for the session
50
     *
51
     * @param int    $websiteId
52
     *
53
     * @return void
54
     * @throws \Exception
55
     */
56
    public function login($customerEmail, $websiteId = 1)
57
    {
58
        /** @var Session $session */
59
        $session = $this->createObject(Session::class, false);
60
        /** @var Customer $customer */
61
        $customer = $this->createObject(Customer::class);
62
        $customer->setWebsiteId($websiteId);
63
        $customerId = $customer->loadByEmail($customerEmail);
64
        if ($session->loginById($customerId->getId()) === false) {
65
            throw new \Exception("Could not log customer in");
0 ignored issues
show
introduced by
Direct throw of Exception is discouraged. Use \Magento\Framework\Exception\LocalizedException instead.
Loading history...
66
        }
67
    }
68
69
    public function loginAdmin($username, $password)
70
    {
71
        /*$session = $this->getAdminSession();*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
72
        /** @var User $user */
73
        $user = $this->createObject(User::class);
74
        $user->loadByUsername($username);
75
        if (null === $user->getId()) {
76
            throw new \Exception('Could not find the admin user');
0 ignored issues
show
introduced by
Direct throw of Exception is discouraged. Use \Magento\Framework\Exception\LocalizedException instead.
Loading history...
77
        }
78
        /*$session->setUser($user);
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
        $session->processLogin();*/
80
0 ignored issues
show
introduced by
Code must not contain multiple empty lines in a row; found 2 empty lines.
Loading history...
81
82
        $auth = $this->createObject(Auth::class, false);
83
        #$session = $auth->getAuthStorage();
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
84
85
        $auth->login($username, $password);
86
        $this->createObject(AuthPlugin::class, false)->afterLogin($auth);
87
88
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 1 blank lines before brace
Loading history...
89
90
    public function createObject($className, $new = true)
91
    {
92
        if ($new === true) {
93
            return $this->_objectManager->create($className);
0 ignored issues
show
introduced by
The direct use of ObjectManager is discouraged. Inject necessary dependencies via constructor.
Loading history...
94
        }
95
96
        return $this->_objectManager->get($className);
0 ignored issues
show
introduced by
The direct use of ObjectManager is discouraged. Inject necessary dependencies via constructor.
Loading history...
97
    }
98
99
    public function assertRedirectsToHomePage()
100
    {
101
        $this->assertRedirect($this->stringEndsWith('index.php/'));
102
    }
103
104
    /**
105
     * @return Proxy
106
     */
107
    public function getAdminSession()
108
    {
109
        $session = $this->createObject(Proxy::class, false);
110
        if ($session->isSessionExists() === false) {
111
            $session->start();
112
        }
113
114
        return $session;
115
    }
116
117
    public function disableSecretKeys()
118
    {
119
        $this->createObject(UrlInterface::class, false)->turnOffSecretKey();
120
    }
121
}
122