2018-10-17 14:26:27 +02:00
|
|
|
<?php
|
2020-09-14 13:11:46 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-09-14 13:11:46 +02:00
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-09-14 13:11:46 +02:00
|
|
|
*/
|
2022-06-21 11:57:17 +02:00
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
2024-09-16 13:13:55 +02:00
|
|
|
*
|
|
|
|
* App\Http\ValidationRules\NewUniqueUserRule
|
2018-10-17 14:26:27 +02:00
|
|
|
*/
|
|
|
|
class UniqueEmailTest extends TestCase
|
|
|
|
{
|
2020-06-25 13:52:04 +02:00
|
|
|
use DatabaseTransactions;
|
|
|
|
|
2018-10-17 14:26:27 +02:00
|
|
|
protected $rule;
|
|
|
|
|
2024-08-22 08:57:52 +02:00
|
|
|
protected 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
|
|
|
|
2020-10-01 12:49:47 +02:00
|
|
|
$ac = Account::factory()->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
|
|
|
|
2020-10-01 12:49:47 +02:00
|
|
|
$company = Company::factory()->make([
|
2018-10-22 14:04:37 +02:00
|
|
|
'account_id' => $account->id,
|
|
|
|
]);
|
|
|
|
|
2019-12-08 11:28:52 +01:00
|
|
|
$company->setHidden(['settings', 'settings_object', 'hashed_id']);
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
Company::on('db-ninja-01')->create($company->toArray());
|
2019-12-08 11:28:52 +01:00
|
|
|
|
2020-10-01 12:49:47 +02:00
|
|
|
$ac2 = Account::factory()->make();
|
2019-12-08 11:28:52 +01:00
|
|
|
$ac2->setHidden(['hashed_id']);
|
|
|
|
$account2 = Account::on('db-ninja-02')->create($ac2->toArray());
|
|
|
|
|
2020-10-01 12:49:47 +02:00
|
|
|
$company2 = Company::factory()->make([
|
2018-10-22 14:04:37 +02:00
|
|
|
'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
|
|
|
];
|
|
|
|
|
2020-06-25 13:52:04 +02:00
|
|
|
$user_find = User::on('db-ninja-01')->where('email', 'user@example.com')->first();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $user_find) {
|
2020-06-25 13:52:04 +02:00
|
|
|
User::on('db-ninja-01')->create($user);
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 13:52:04 +02:00
|
|
|
$user_find = User::on('db-ninja-02')->where('email', 'user@example.com')->first();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $user_find) {
|
2020-06-25 13:52:04 +02:00
|
|
|
User::on('db-ninja-02')->create($user2);
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
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'));
|
|
|
|
}
|
|
|
|
|
2024-08-22 08:57:52 +02:00
|
|
|
protected function tearDown(): void
|
2020-03-21 06:37:30 +01:00
|
|
|
{
|
2024-09-03 17:32:29 +02:00
|
|
|
parent::tearDown();
|
|
|
|
|
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
|
|
|
}
|