-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
RegistrationTest.php
132 lines (112 loc) · 4.02 KB
/
RegistrationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* UserFrosting (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/UserFrosting
* @copyright Copyright (c) 2019 Alexander Weissman
* @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License)
*/
namespace UserFrosting\Sprinkle\Account\Tests\Unit;
use Mockery as m;
use UserFrosting\Tests\TestCase;
use UserFrosting\Sprinkle\Core\Tests\TestDatabase;
use UserFrosting\Sprinkle\Core\Tests\RefreshDatabase;
use UserFrosting\Sprinkle\Account\Account\Registration;
use UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface;
use UserFrosting\Support\Exception\HttpException;
/**
* RegistrationTest Class
* Tests the Registration class.
*/
class RegistrationTest extends TestCase
{
use TestDatabase;
use RefreshDatabase;
public function tearDown()
{
parent::tearDown();
m::close();
}
/**
* Setup the database schema.
*/
public function setUp()
{
parent::setUp();
// Setup test database
$this->setupTestDatabase();
$this->refreshDatabase();
}
/**
* testNormalRegistration
*/
public function testNormalRegistration()
{
// userActivityLogger will receive something, but won't be able to handle it since there's no session. So we mock it
$this->ci->userActivityLogger = m::mock('\Monolog\Logger');
$this->ci->userActivityLogger->shouldReceive('info')->once();
// Tests can't mail properly
$this->ci->config['site.registration.require_email_verification'] = false;
// Genereate user data
$fakeUserData = [
'user_name' => 'FooBar',
'first_name' => 'Foo',
'last_name' => 'Bar',
'email' => 'Foo@Bar.com',
'password' => 'FooBarFooBar123'
];
// Get class
$registration = new Registration($this->ci, $fakeUserData);
$this->assertInstanceOf(Registration::class, $registration);
// Register user
$user = $registration->register();
// Registration should return a valid user
$this->assertInstanceOf(UserInterface::class, $user);
$this->assertEquals('FooBar', $user->user_name);
// We try to register the same user again. Should throw an error
$registration = new Registration($this->ci, $fakeUserData);
$this->expectException(HttpException::class);
$validation = $registration->validate();
// Should throw email error if we change the username
$fakeUserData['user_name'] = 'BarFoo';
$registration = new Registration($this->ci, $fakeUserData);
$this->expectException(HttpException::class);
$validation = $registration->validate();
}
/**
* Test validation works
*/
public function testValidation()
{
// Reset database
$this->refreshDatabase();
$registration = new Registration($this->ci, [
'user_name' => 'FooBar',
'first_name' => 'Foo',
'last_name' => 'Bar',
'email' => 'Foo@Bar.com',
'password' => 'FooBarFooBar123'
]);
// Validate user. Shouldn't tell us the username is already in use since we reset the database
$validation = $registration->validate();
$this->assertTrue($validation);
}
/**
* Test the $requiredProperties property
*/
public function testMissingFields()
{
// Reset database
$this->refreshDatabase();
$registration = new Registration($this->ci, [
'user_name' => 'FooBar',
//'first_name' => 'Foo',
'last_name' => 'Bar',
'email' => 'Foo@Bar.com',
'password' => 'FooBarFooBar123'
]);
// Validate user. Shouldn't tell us the username is already in use since we reset the database
$this->expectException(HttpException::class);
$validation = $registration->validate();
}
}