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