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') |
|
|
|
|
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') |
|
|
|
|
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
|
|
|
|