1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-06 11:12:43 +01:00
invoiceninja/tests/Integration/UniqueEmailTest.php

112 lines
3.0 KiB
PHP
Raw Normal View History

<?php
2020-09-14 13:11:46 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-09-14 13:11:46 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace Tests\Integration;
2019-06-05 07:33:48 +02:00
use App\Http\ValidationRules\NewUniqueUserRule;
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;
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
*/
class UniqueEmailTest extends TestCase
{
2020-06-25 13:52:04 +02:00
use DatabaseTransactions;
protected $rule;
2019-04-24 12:01:40 +02:00
public function setUp() :void
{
parent::setUp();
User::unguard();
if (! config('ninja.db.multi_db_enabled')) {
$this->markTestSkipped('Multi DB not enabled - skipping');
}
2019-06-05 07:33:48 +02:00
$this->rule = new NewUniqueUserRule();
2020-10-01 12:49:47 +02:00
$ac = Account::factory()->make();
2019-07-31 12:37:55 +02:00
$ac->setHidden(['hashed_id']);
$account = Account::on('db-ninja-01')->create($ac->toArray());
2020-10-01 12:49:47 +02:00
$company = Company::factory()->make([
'account_id' => $account->id,
]);
$company->setHidden(['settings', 'settings_object', 'hashed_id']);
Company::on('db-ninja-01')->create($company->toArray());
2020-10-01 12:49:47 +02:00
$ac2 = Account::factory()->make();
$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([
'account_id' => $account2->id,
]);
2019-07-31 12:37:55 +02:00
$company2->setHidden(['settings', 'settings_object', 'hashed_id']);
Company::on('db-ninja-02')->create($company2->toArray());
$user = [
'first_name' => 'user_db_1',
'email' => 'user@example.com',
'password' => Hash::make('password'),
'account_id' => $account->id,
];
$user2 = [
'first_name' => 'user_db_2',
'email' => 'user@example.com',
'password' => Hash::make('password'),
'account_id' => $account2->id,
];
2020-06-25 13:52:04 +02:00
$user_find = User::on('db-ninja-01')->where('email', 'user@example.com')->first();
if (! $user_find) {
2020-06-25 13:52:04 +02:00
User::on('db-ninja-01')->create($user);
}
2020-06-25 13:52:04 +02:00
$user_find = User::on('db-ninja-02')->where('email', 'user@example.com')->first();
if (! $user_find) {
2020-06-25 13:52:04 +02:00
User::on('db-ninja-02')->create($user2);
}
}
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
{
DB::connection('db-ninja-01')->table('users')->delete();
DB::connection('db-ninja-02')->table('users')->delete();
}
}