-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathRegistrationTest.php
155 lines (134 loc) · 4.78 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?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\Sprinkle\Account\Database\Models\User;
/**
* RegistrationTest Class
* Tests the Registration class.
*/
class RegistrationTest extends TestCase
{
use TestDatabase;
use RefreshDatabase;
/**
* @var array Test user data
*/
protected $fakeUserData = [
'user_name' => 'FooBar',
'first_name' => 'Foo',
'last_name' => 'Bar',
'email' => 'Foo@Bar.com',
'password' => 'FooBarFooBar123'
];
public function tearDown()
{
parent::tearDown();
m::close();
}
/**
* Setup the database schema.
*/
public function setUp()
{
parent::setUp();
// Setup test database
$this->setupTestDatabase();
$this->refreshDatabase();
}
/**
* Test validation works
*/
public function testValidation()
{
$registration = new Registration($this->ci, [
'user_name' => 'OwlFancy',
'first_name' => 'Owl',
'last_name' => 'Fancy',
'email' => 'owl@fancy.com',
'password' => 'owlFancy1234'
]);
$validation = $registration->validate();
$this->assertTrue($validation);
}
/**
* Test the $requiredProperties property
* @depends testValidation
* @expectedException UserFrosting\Support\Exception\HttpException
* @expectedExceptionMessage Account can't be registrated as 'first_name' is required to create a new user.
*/
public function testMissingFields()
{
$registration = new Registration($this->ci, [
'user_name' => 'OwlFancy',
//'first_name' => 'Owl',
'last_name' => 'Fancy',
'email' => 'owl@fancy.com',
'password' => 'owlFancy1234'
]);
$validation = $registration->validate();
}
/**
* @depends testValidation
*/
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;
// Get class
$registration = new Registration($this->ci, $this->fakeUserData);
$this->assertInstanceOf(Registration::class, $registration);
// Register user
$user = $registration->register();
// Registration should return a valid user, with a new ID
$this->assertInstanceOf(UserInterface::class, $user);
$this->assertEquals('FooBar', $user->user_name);
$this->assertInternalType('int', $user->id);
// Make sure the user is added to the db by querying it
$users = User::where('email', 'Foo@Bar.com')->get();
$this->assertCount(1, $users);
$this->assertSame('FooBar', $users->first()['user_name']);
}
/**
* @depends testNormalRegistration
* @expectedException UserFrosting\Support\Exception\HttpException
* @expectedExceptionMessage Username is already in use.
*/
public function testValidationWithDuplicateUsername()
{
// Create the first user to test against
$this->testNormalRegistration();
// We try to register the same user again. Should throw an error
$registration = new Registration($this->ci, $this->fakeUserData);
$validation = $registration->validate();
}
/**
* @depends testNormalRegistration
* @expectedException UserFrosting\Support\Exception\HttpException
* @expectedExceptionMessage Email is already in use.
*/
public function testValidationWithDuplicateEmail()
{
// Create the first user to test against
$this->testNormalRegistration();
// Should throw email error if we change the username
$fakeUserData = $this->fakeUserData;
$fakeUserData['user_name'] = 'BarFoo';
$registration = new Registration($this->ci, $fakeUserData);
$validation = $registration->validate();
}
}