2018-10-17 14:26:27 +02:00
|
|
|
<?php
|
|
|
|
|
2019-05-14 06:05:05 +02:00
|
|
|
namespace Tests\Integration;
|
2018-10-17 14:26:27 +02:00
|
|
|
|
2019-06-05 07:33:48 +02:00
|
|
|
use App\Http\ValidationRules\NewUniqueUserRule;
|
2018-10-22 14:04:37 +02:00
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Company;
|
2019-04-24 12:01:40 +02:00
|
|
|
use App\Models\User;
|
2018-10-17 14:26:27 +02:00
|
|
|
use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase;
|
2019-04-24 12:01:40 +02:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2018-10-17 14:26:27 +02:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2019-06-05 07:33:48 +02:00
|
|
|
* @covers App\Http\ValidationRules\NewUniqueUserRule
|
2018-10-17 14:26:27 +02:00
|
|
|
*/
|
|
|
|
class UniqueEmailTest extends TestCase
|
|
|
|
{
|
|
|
|
protected $rule;
|
|
|
|
|
2019-04-24 12:01:40 +02:00
|
|
|
public function setUp() :void
|
2018-10-17 14:26:27 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2019-12-08 11:28:52 +01:00
|
|
|
User::unguard();
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2018-10-17 14:26:27 +02:00
|
|
|
$this->markTestSkipped('Multi DB not enabled - skipping');
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2018-10-17 14:26:27 +02:00
|
|
|
|
2019-06-05 07:33:48 +02:00
|
|
|
$this->rule = new NewUniqueUserRule();
|
2018-10-17 14:26:27 +02:00
|
|
|
|
2018-10-22 14:04:37 +02:00
|
|
|
$ac = factory(\App\Models\Account::class)->make();
|
2019-07-31 12:37:55 +02:00
|
|
|
$ac->setHidden(['hashed_id']);
|
|
|
|
|
2018-10-24 12:24:09 +02:00
|
|
|
$account = Account::on('db-ninja-01')->create($ac->toArray());
|
2018-10-22 14:04:37 +02:00
|
|
|
|
|
|
|
$company = factory(\App\Models\Company::class)->make([
|
|
|
|
'account_id' => $account->id,
|
|
|
|
]);
|
|
|
|
|
2019-12-08 11:28:52 +01:00
|
|
|
$company->setHidden(['settings', 'settings_object', 'hashed_id']);
|
|
|
|
|
|
|
|
Company::on('db-ninja-01')->create($company->toArray());
|
|
|
|
|
|
|
|
|
|
|
|
$ac2 = factory(\App\Models\Account::class)->make();
|
|
|
|
$ac2->setHidden(['hashed_id']);
|
|
|
|
$account2 = Account::on('db-ninja-02')->create($ac2->toArray());
|
|
|
|
|
2018-10-22 14:04:37 +02:00
|
|
|
$company2 = factory(\App\Models\Company::class)->make([
|
|
|
|
'account_id' => $account2->id,
|
|
|
|
]);
|
|
|
|
|
2019-07-31 12:37:55 +02:00
|
|
|
$company2->setHidden(['settings', 'settings_object', 'hashed_id']);
|
2019-02-17 11:34:46 +01:00
|
|
|
|
2018-10-24 12:24:09 +02:00
|
|
|
Company::on('db-ninja-02')->create($company2->toArray());
|
2018-10-22 14:04:37 +02:00
|
|
|
|
|
|
|
|
2018-10-17 14:26:27 +02:00
|
|
|
$user = [
|
2018-10-22 14:04:37 +02:00
|
|
|
'first_name' => 'user_db_1',
|
|
|
|
'email' => 'user@example.com',
|
|
|
|
'password' => Hash::make('password'),
|
2020-03-24 10:15:30 +01:00
|
|
|
'account_id' => $account->id,
|
2018-10-22 14:04:37 +02:00
|
|
|
];
|
2018-10-19 05:45:55 +02:00
|
|
|
|
2018-10-22 14:04:37 +02:00
|
|
|
$user2 = [
|
|
|
|
'first_name' => 'user_db_2',
|
|
|
|
'email' => 'user@example.com',
|
|
|
|
'password' => Hash::make('password'),
|
2020-03-24 10:15:30 +01:00
|
|
|
'account_id' => $account2->id,
|
2018-10-17 14:26:27 +02:00
|
|
|
];
|
|
|
|
|
2018-10-24 12:24:09 +02:00
|
|
|
User::on('db-ninja-01')->create($user);
|
|
|
|
User::on('db-ninja-02')->create($user2);
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_unique_emails_detected_on_database()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->rule->passes('email', 'user@example.com'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_no_unique_emails_detected()
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->rule->passes('email', 'nohit@example.com'));
|
|
|
|
}
|
|
|
|
|
2019-04-24 12:01:40 +02:00
|
|
|
public function tearDown() :void
|
2020-03-21 06:37:30 +01:00
|
|
|
{
|
|
|
|
DB::connection('db-ninja-01')->table('users')->delete();
|
|
|
|
DB::connection('db-ninja-02')->table('users')->delete();
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|