Failed Conditions
Push — master ( 1a6d63...dcbc0d )
by Adrien
05:26
created

UserControllerTest::testNewAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 27
c 3
b 0
f 0
dl 0
loc 45
rs 9.488
cc 4
nc 4
nop 0
1
<?php
2
3
class UserControllerTest extends AbstractControllerTestCase
4
{
5
    protected $idUser;
6
    protected $newUserData = [
7
        'nickname' => 'new test user',
8
        'email' => '[email protected]',
9
        'password' => 'superpassword',
10
    ];
11
12
    public function tearDown(): void
13
    {
14
        $user = \mQueue\Model\UserMapper::findEmailPassword($this->newUserData['email'], $this->newUserData['password']);
15
        if ($user) {
16
            $user->delete();
17
        }
18
19
        parent::tearDown();
20
    }
21
22
    public function testIndexAction(): void
23
    {
24
        $params = ['action' => 'index', 'controller' => 'user', 'module' => 'default'];
25
        $url = $this->url($this->urlizeOptions($params));
26
        $this->dispatch($url);
27
28
        // assertions
29
        $this->assertModule($params['module']);
30
        $this->assertController($params['controller']);
31
        $this->assertAction($params['action']);
32
33
        $this->assertQueryContentContains('h2', 'Users list');
34
    }
35
36
    public function testNewAction(): void
37
    {
38
        // First, query to display form
39
        $params = ['action' => 'new', 'controller' => 'user', 'module' => 'default'];
40
        $url = $this->url($this->urlizeOptions($params));
41
        $this->dispatch($url);
42
43
        // assertions
44
        $this->assertModule($params['module']);
45
        $this->assertController($params['controller']);
46
        $this->assertAction($params['action']);
47
48
        $this->assertQueryContentContains('h2', 'Create new user');
49
        $this->assertQueryContentContains('form', 'Subscribe');
50
51
        // Find out csrf value to be re-used in POST
52
        $captcha = null;
53
        $captchaId = null;
54
        $csrf = null;
55
        foreach ($_SESSION as $key => $q) {
56
            if (preg_match('~^Zend_Form_Captcha_(.*)$~', $key, $m)) {
57
                $captcha = $q['word'];
58
                $captchaId = $m[1];
59
            } elseif ($key === 'Zend_Form_Element_Hash_salt_csrf') {
60
                $csrf = $q['hash'];
61
            }
62
        }
63
64
        // Reset everything
65
        $this->resetRequest();
66
        $this->resetResponse();
67
68
        // Prepare POST query
69
        $this->newUserData['captcha'] = [
70
            'id' => $captchaId,
71
            'input' => $captcha,
72
        ];
73
        $this->newUserData['csrf'] = $csrf;
74
        $this->request->setMethod('POST')
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist on UserControllerTest. Since you implemented __get, consider adding a @property annotation.
Loading history...
75
            ->setPost($this->newUserData);
76
77
        // Subscribe new test user
78
        $this->dispatch($url);
79
80
        $this->assertRedirectTo('/movie', 'succesfull subscription redirect to movie list');
81
    }
82
83
    public function testLoginAction(): void
84
    {
85
        $this->assertNull(\mQueue\Model\User::getCurrent(), 'at first we are not logged in');
86
87
        // Create test user
88
        $this->testNewAction();
89
90
        $this->assertNotNull(\mQueue\Model\User::getCurrent(), 'after subscription, we are automatically logged in');
91
92
        // Reset everything
93
        $this->resetRequest();
94
        $this->resetResponse();
95
96
        $params = ['action' => 'logout', 'controller' => 'user', 'module' => 'default'];
97
        $url = $this->url($this->urlizeOptions($params));
98
        $this->dispatch($url);
99
100
        $this->assertNull(\mQueue\Model\User::getCurrent(), 'after logged out, we are logged out');
101
102
        // Reset everything
103
        $this->resetRequest();
104
        $this->resetResponse();
105
106
        // Prepare POST query
107
        $this->request->setMethod('POST')
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist on UserControllerTest. Since you implemented __get, consider adding a @property annotation.
Loading history...
108
            ->setPost($this->newUserData);
109
110
        $params = ['action' => 'login', 'controller' => 'user', 'module' => 'default'];
111
        $url = $this->url($this->urlizeOptions($params));
112
        $this->dispatch($url);
113
114
        $this->assertNotNull(\mQueue\Model\User::getCurrent(), 'after login, we are login');
115
    }
116
}
117