1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/tests/Unit/CheckDataTest.php

284 lines
8.3 KiB
PHP
Raw Normal View History

2023-09-04 03:04:51 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace Tests\Unit;
2023-11-26 08:41:42 +01:00
use App\DataMapper\CompanySettings;
use App\Factory\CompanyUserFactory;
2023-09-04 03:04:51 +02:00
use App\Models\Account;
2023-11-26 08:41:42 +01:00
use App\Models\Client;
use App\Models\ClientContact;
2023-09-04 03:04:51 +02:00
use App\Models\Company;
2023-11-26 08:41:42 +01:00
use App\Models\CompanyToken;
2023-09-04 03:04:51 +02:00
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\Paymentable;
2023-11-26 08:41:42 +01:00
use App\Models\User;
2023-09-04 03:04:51 +02:00
use Illuminate\Routing\Middleware\ThrottleRequests;
2023-11-26 08:41:42 +01:00
use Tests\TestCase;
2023-09-04 03:04:51 +02:00
/**
2024-09-16 13:13:55 +02:00
*
2023-09-04 03:04:51 +02:00
*/
class CheckDataTest extends TestCase
{
protected $account;
protected $user;
protected $company;
protected $cu;
protected $token;
protected $client;
protected $faker;
/**
* Important consideration with Base64
* encoding checks.
*
* No method can guarantee against false positives.
*/
2024-08-22 08:57:52 +02:00
protected function setUp(): void
2023-09-04 03:04:51 +02:00
{
parent::setUp();
2024-08-22 08:57:52 +02:00
2023-09-04 03:04:51 +02:00
$this->faker = \Faker\Factory::create();
$this->withoutMiddleware(
ThrottleRequests::class
);
}
private function buildData()
{
$this->account = Account::factory()->create([
'hosted_client_count' => 1000,
'hosted_company_count' => 1000,
]);
$this->account->num_users = 3;
$this->account->save();
$this->user = User::factory()->create([
'account_id' => $this->account->id,
'confirmation_code' => 'xyz123',
'email' => $this->faker->unique()->safeEmail(),
]);
$settings = CompanySettings::defaults();
$settings->client_online_payment_notification = false;
$settings->client_manual_payment_notification = false;
$this->company = Company::factory()->create([
'account_id' => $this->account->id,
'settings' => $settings,
]);
$this->company->settings = $settings;
$this->company->save();
$this->cu = CompanyUserFactory::create($this->user->id, $this->company->id, $this->account->id);
$this->cu->is_owner = true;
$this->cu->is_admin = true;
$this->cu->is_locked = false;
$this->cu->save();
$this->token = \Illuminate\Support\Str::random(64);
2024-08-22 08:57:52 +02:00
$company_token = new CompanyToken();
2023-09-04 03:04:51 +02:00
$company_token->user_id = $this->user->id;
$company_token->company_id = $this->company->id;
$company_token->account_id = $this->account->id;
$company_token->name = 'test token';
$company_token->token = $this->token;
$company_token->is_system = true;
$company_token->save();
$this->client = Client::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'is_deleted' => 0,
'name' => 'bob',
'address1' => '1234',
'balance' => 100,
'paid_to_date' => 50,
]);
ClientContact::factory()->create([
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'company_id' => $this->company->id,
'is_primary' => 1,
'first_name' => 'john',
'last_name' => 'doe',
'email' => 'john@doe.com'
]);
}
public function testDbQueriesRaw5()
{
$this->buildData();
$i = Invoice::factory()->count(5)->create([
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'company_id' => $this->company->id,
]);
Invoice::where('status_id', 2)->cursor()->each(function ($i) {
$i->service()->markPaid()->save();
});
2023-11-26 08:41:42 +01:00
Payment::with('paymentables')->cursor()->each(function ($payment) {
2023-09-04 03:04:51 +02:00
$this->assertNotNull($payment->paymentables()->where('paymentable_type', \App\Models\Credit::class)->get()
->sum(\DB::raw('amount')->getValue(\DB::connection()->getQueryGrammar())));
});
2024-08-22 08:57:52 +02:00
2023-09-04 03:04:51 +02:00
Payment::with('paymentables')->cursor()->each(function ($payment) {
$this->assertNotNull($payment->paymentables()->where('paymentable_type', \App\Models\Credit::class)->get()
->sum('amount'));
});
$amount = Paymentable::first()->payment->paymentables()->where('paymentable_type', 'invnoices')->get()->sum('amount');
$this->assertNotNull($amount);
}
public function testDbQueriesRaw4()
{
$this->buildData();
ClientContact::factory()->count(10)->create([
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'company_id' => $this->company->id,
]);
$clients_refactor = \DB::table('clients')
2023-11-26 08:41:42 +01:00
->leftJoin('client_contacts', function ($join) {
$join->on('client_contacts.client_id', '=', 'clients.id');
2023-09-04 03:04:51 +02:00
})
->get(['clients.id', \DB::raw('count(client_contacts.id) as contact_count')]);
// $this->assertNotNull($clients);
$this->assertNotNull($clients_refactor);
}
public function testDbQueriesRaw3()
{
$this->buildData();
User::factory()->create([
'account_id' => $this->account->id,
'email' => $this->faker->unique()->safeEmail(),
]);
2024-08-22 08:57:52 +02:00
2023-09-04 03:04:51 +02:00
User::factory()->create([
'account_id' => $this->account->id,
'email' => $this->faker->unique()->safeEmail(),
]);
$user_hash = 'a';
$user_count = User::where('account_id', $this->company->account->id)
->where(
\DB::raw('CONCAT_WS(" ", first_name, last_name)'),
'like',
'%'.$user_hash.'%'
)
->get();
$user_count_refactor = User::whereRaw("account_id = ? AND CONCAT_WS(' ', first_name, last_name) like ?", [$this->company->account_id, '%'.$user_hash.'%'])
->get();
$this->assertEquals($user_count_refactor->count(), $user_count->count());
}
public function testDbRawQueries1()
{
$this->buildData();
$results = \DB::select(\DB::raw("
SELECT count(clients.id) as count
FROM clients
")->getValue(\DB::connection()->getQueryGrammar()));
2024-08-22 08:57:52 +02:00
2023-09-04 03:04:51 +02:00
$refactored = \DB::select("
SELECT count(clients.id) as count
FROM clients
");
$this->assertEquals($refactored[0]->count, $results[0]->count);
}
public function testDbRawQueries2()
{
$this->buildData();
Payment::factory()->count(5)->create([
'client_id' => $this->client->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
]);
Invoice::factory()->count(5)->create([
'client_id' => $this->client->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
]);
Invoice::where('status_id', 2)->cursor()->each(function ($i) {
$i->service()->markPaid()->save();
});
$results = \DB::select(\DB::raw("
SELECT
SUM(payments.amount) as amount
FROM payments
LEFT JOIN paymentables
ON
payments.id = paymentables.payment_id
WHERE paymentable_type = ?
AND paymentables.deleted_at is NULL
AND paymentables.amount > 0
AND payments.is_deleted = 0
AND payments.client_id = ?;
")->getValue(\DB::connection()->getQueryGrammar()), ['invoices', $this->client->id]);
2024-08-22 08:57:52 +02:00
2023-09-04 03:04:51 +02:00
$refactored = \DB::select("
SELECT
SUM(payments.amount) as amount
FROM payments
LEFT JOIN paymentables
ON
payments.id = paymentables.payment_id
WHERE paymentable_type = ?
AND paymentables.deleted_at is NULL
AND paymentables.amount > 0
AND payments.is_deleted = 0
AND payments.client_id = ?;
", ['invoices', $this->client->id]);
$this->assertEquals($refactored[0]->amount, $results[0]->amount);
}
}