1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/tests/Feature/Bank/BankTransactionRuleTest.php

797 lines
23 KiB
PHP
Raw Normal View History

2022-11-20 03:55:19 +01: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\Feature\Bank;
use App\Models\BankIntegration;
use App\Models\BankTransaction;
use App\Models\BankTransactionRule;
use Illuminate\Foundation\Testing\DatabaseTransactions;
2022-11-21 21:14:00 +01:00
use Illuminate\Validation\ValidationException;
2022-11-20 03:55:19 +01:00
use Tests\MockAccountData;
use Tests\TestCase;
class BankTransactionRuleTest extends TestCase
{
use DatabaseTransactions;
use MockAccountData;
protected function setUp() :void
{
parent::setUp();
$this->makeTestData();
$this->withoutMiddleware(
ThrottleRequests::class
);
2022-11-21 21:14:00 +01:00
$this->withoutExceptionHandling();
2022-11-20 03:55:19 +01:00
}
2023-01-23 02:23:05 +01:00
public function testMatchingWithStripos()
{
$bt_value = strtolower(str_replace(" ", "", 'hello soldier'));
$rule_value = strtolower(str_replace(" ", "", 'solider'));
$rule_length = iconv_strlen($rule_value);
2023-02-16 02:36:09 +01:00
$this->assertFalse(stripos($rule_value, $bt_value) !== false);
2023-01-23 02:23:05 +01:00
$this->assertFalse(stripos($bt_value, $rule_value) !== false);
}
public function testBankRuleBulkActions()
{
$data = [
'action' => 'archive',
'ids' => [$this->bank_transaction_rule]
];
$response = $this->withHeaders([
'X-API-TOKEN' => $this->token,
])->post('/api/v1/bank_transaction_rules/bulk', $data)
->assertStatus(200);
$data = [
'ids' => [$this->bank_transaction_rule->hashed_id],
'action' => 'restore'
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/bank_transaction_rules/bulk', $data)
->assertStatus(200);
$data = [
'ids' => [$this->bank_transaction_rule->hashed_id],
'action' => 'delete'
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/bank_transaction_rules/bulk', $data)
->assertStatus(200);
}
2022-11-23 12:14:36 +01:00
public function testValidationContainsRule()
{
2022-12-01 02:41:28 +01:00
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => 'HellO ThErE CowBoY',
'base_type' => 'DEBIT',
'amount' => 100
]);
2022-11-23 12:14:36 +01:00
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'DEBIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'category_id' =>$this->expense_category->id,
'rules' => [
[
'search_key' => 'description',
'operator' => 'contains',
'value' => 'hello',
]
]
2023-02-16 02:36:09 +01:00
]);
2022-11-23 12:14:36 +01:00
2022-12-01 02:41:28 +01:00
$bt = $bt->refresh();
2022-11-23 12:14:36 +01:00
2022-12-01 02:41:28 +01:00
$debit_rules = $bt->company->debit_rules();
2022-11-23 12:14:36 +01:00
$bt->service()->processRules();
$bt = $bt->fresh();
$this->assertNotNull($bt->expense_id);
2023-04-06 03:38:29 +02:00
// $this->assertNotNull($bt->expense->category_id);
// $this->assertNotNull($bt->expense->vendor_id);
2022-11-23 12:14:36 +01:00
2022-12-01 02:41:28 +01:00
$bt = null;
2022-11-23 12:14:36 +01:00
}
2022-11-21 21:10:17 +01:00
public function testUpdateValidationRules()
{
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'DEBIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'rules' => [
[
'search_key' => 'amount',
'operator' => '<=',
'value' => 100,
]
]
]);
$data = [
2023-02-16 02:36:09 +01:00
"applies_to" => "DEBIT",
"archived_at" => 0,
"auto_convert" => false,
"category_id" => $this->expense_category->hashed_id,
"is_deleted" => false,
"isChanged" => true,
"matches_on_all" => true,
"name" => "TEST 22",
"updated_at" => 1669060432,
2022-11-21 21:10:17 +01:00
"vendor_id" => $this->vendor->hashed_id
];
2022-11-21 21:14:00 +01:00
$response = null;
2022-11-21 21:10:17 +01:00
2022-11-21 21:14:00 +01:00
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
2023-02-07 21:31:24 +01:00
])->putJson('/api/v1/bank_transaction_rules/'. $br->hashed_id. '?include=expense_category', $data);
2022-11-21 21:14:00 +01:00
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
nlog($message);
}
2022-11-21 21:10:17 +01:00
2023-02-16 02:36:09 +01:00
if ($response) {
2022-11-21 21:14:00 +01:00
$arr = $response->json();
2023-04-28 08:47:22 +02:00
2023-02-16 02:36:09 +01:00
$response->assertStatus(200);
2022-11-21 21:14:00 +01:00
}
2022-11-21 21:10:17 +01:00
}
2022-11-20 04:31:30 +01:00
public function testMatchingBankTransactionExpenseAmountLessThanEqualTo()
{
2022-12-01 02:41:28 +01:00
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => 'xx',
'base_type' => 'DEBIT',
'amount' => 100
]);
2022-11-20 04:31:30 +01:00
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'DEBIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'rules' => [
[
'search_key' => 'amount',
'operator' => '<=',
'value' => 100,
]
]
]);
2022-12-01 02:41:28 +01:00
2022-11-20 04:31:30 +01:00
2022-12-01 02:41:28 +01:00
$bt->company->refresh();
2022-11-20 04:31:30 +01:00
2022-12-01 02:41:28 +01:00
$bt->refresh()->service()->processRules();
2022-11-20 04:31:30 +01:00
$bt = $bt->fresh();
$this->assertNotNull($bt->expense_id);
2022-12-01 02:41:28 +01:00
$bt=null;
2022-11-20 04:31:30 +01:00
}
public function testMatchingBankTransactionExpenseAmountLessThan()
{
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'DEBIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'rules' => [
[
'search_key' => 'amount',
'operator' => '<',
'value' => 100,
]
]
]);
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => '',
'base_type' => 'DEBIT',
'amount' => 99
]);
$bt->service()->processRules();
$bt = $bt->fresh();
$this->assertNotNull($bt->expense_id);
}
public function testMatchingBankTransactionExpenseAmountGreaterThan()
{
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'DEBIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'rules' => [
[
'search_key' => 'amount',
'operator' => '>',
'value' => 100,
]
]
]);
2022-11-20 04:19:35 +01:00
2022-11-20 04:31:30 +01:00
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => '',
'base_type' => 'DEBIT',
'amount' => 101
]);
2022-12-01 02:41:28 +01:00
$bt->refresh()->service()->processRules();
2022-11-20 04:31:30 +01:00
$bt = $bt->fresh();
$this->assertNotNull($bt->expense_id);
}
public function testMatchingBankTransactionExpenseAmountMiss()
{
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'DEBIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'rules' => [
[
'search_key' => 'amount',
'operator' => '=',
'value' => 100,
]
]
]);
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => '',
'base_type' => 'DEBIT',
'amount' => 101
]);
$bt->service()->processRules();
$bt = $bt->fresh();
2023-04-06 03:38:29 +02:00
$this->assertEmpty($bt->expense_id);
2022-11-20 04:31:30 +01:00
}
public function testMatchingBankTransactionExpenseAmount()
{
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'DEBIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'rules' => [
[
'search_key' => 'amount',
'operator' => '=',
'value' => 100,
]
]
]);
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => '',
'base_type' => 'DEBIT',
'amount' => 100
]);
2022-11-20 04:21:35 +01:00
2022-11-20 04:31:30 +01:00
$bt->service()->processRules();
$bt = $bt->fresh();
$this->assertNotNull($bt->expense_id);
}
2022-11-20 04:21:35 +01:00
public function testMatchingBankTransactionExpenseIsEmpty()
{
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'DEBIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'rules' => [
[
'search_key' => 'description',
'operator' => 'is_empty',
'value' => '',
]
]
]);
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => '',
'base_type' => 'DEBIT',
'amount' => 100
]);
2022-12-01 02:41:28 +01:00
$bt = $bt->refresh();
2022-11-20 04:21:35 +01:00
$bt->service()->processRules();
$bt = $bt->fresh();
$this->assertNotNull($bt->expense_id);
}
public function testMatchingBankTransactionExpenseIsEmptyMiss()
{
2022-12-01 02:41:28 +01:00
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => 'asdadsa',
'base_type' => 'DEBIT',
'amount' => 100
]);
2022-11-20 04:21:35 +01:00
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'DEBIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'rules' => [
[
'search_key' => 'description',
'operator' => 'is_empty',
'value' => '',
]
]
]);
2022-12-01 02:41:28 +01:00
$bt->load('company');
2022-11-20 04:21:35 +01:00
$bt->service()->processRules();
$bt = $bt->fresh();
2023-04-06 03:38:29 +02:00
$this->assertEmpty($bt->expense_id);
2022-11-20 04:21:35 +01:00
}
2022-11-20 04:19:35 +01:00
public function testMatchingBankTransactionExpenseStartsWithMiss()
{
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'DEBIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'rules' => [
[
'search_key' => 'description',
'operator' => 'starts_with',
'value' => 'chesst',
]
]
]);
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => 'ChESSSty coughs are terrible',
'base_type' => 'DEBIT',
'amount' => 100
]);
$bt->service()->processRules();
$bt = $bt->fresh();
2023-04-06 03:38:29 +02:00
$this->assertEmpty($bt->expense_id);
2022-11-20 04:19:35 +01:00
}
public function testMatchingBankTransactionExpenseStartsWith()
{
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'DEBIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'rules' => [
[
'search_key' => 'description',
'operator' => 'starts_with',
'value' => 'chess',
]
]
]);
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => 'ChESSSty coughs are terrible',
'base_type' => 'DEBIT',
'amount' => 100
]);
$bt->service()->processRules();
$bt = $bt->fresh();
$this->assertNotNull($bt->expense_id);
}
public function testMatchingBankTransactionExpenseContainsMiss()
2023-02-16 02:36:09 +01:00
{
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'DEBIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'rules' => [
[
'search_key' => 'description',
'operator' => 'contains',
'value' => 'asdddfd',
]
]
]);
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => 'Something asd bizarre',
'base_type' => 'DEBIT',
'amount' => 100
]);
2022-11-20 04:19:35 +01:00
2023-02-16 02:36:09 +01:00
$bt->service()->processRules();
2022-11-20 04:19:35 +01:00
2023-02-16 02:36:09 +01:00
$bt = $bt->fresh();
2022-11-20 04:19:35 +01:00
2023-04-06 03:38:29 +02:00
$this->assertEmpty($bt->expense_id);
2023-02-16 02:36:09 +01:00
}
2022-11-20 04:19:35 +01:00
public function testMatchingBankTransactionExpenseContains()
{
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'DEBIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'rules' => [
[
'search_key' => 'description',
'operator' => 'contains',
'value' => 'asd',
]
]
]);
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => 'Something asd bizarre',
'base_type' => 'DEBIT',
'amount' => 100
]);
$bt->service()->processRules();
$bt = $bt->fresh();
$this->assertNotNull($bt->expense_id);
}
public function testMatchingBankTransactionExpenseMiss()
{
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'DEBIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'rules' => [
[
'search_key' => 'description',
'operator' => 'is',
'value' => 'wallaby',
]
]
]);
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => 'Wall',
'base_type' => 'DEBIT',
]);
$bt->service()->processRules();
$bt = $bt->fresh();
2023-04-06 03:38:29 +02:00
$this->assertEmpty($bt->expense_id);
2022-11-20 04:19:35 +01:00
}
public function testMatchingBankTransactionExpense()
{
2022-11-20 03:55:19 +01:00
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'DEBIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'rules' => [
[
'search_key' => 'description',
'operator' => 'is',
'value' => 'wallaby',
]
]
]);
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => 'WallABy',
'base_type' => 'DEBIT',
]);
$bt->service()->processRules();
$bt = $bt->fresh();
$this->assertNotNull($bt->expense_id);
}
2022-11-20 04:19:35 +01:00
public function testMatchingBankTransactionInvoice()
{
$this->invoice->number = "MUHMUH";
$this->invoice->save();
$br = BankTransactionRule::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'matches_on_all' => false,
'auto_convert' => true,
'applies_to' => 'CREDIT',
'client_id' => $this->client->id,
'vendor_id' => $this->vendor->id,
'rules' => [
[
'search_key' => 'description',
'operator' => 'is',
'value' => 'MUHMUH',
]
]
]);
$bi = BankIntegration::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'account_id' => $this->account->id,
]);
$bt = BankTransaction::factory()->create([
'bank_integration_id' => $bi->id,
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'description' => 'MUHMUH',
'base_type' => 'CREDIT',
'amount' => 100
]);
$bt->service()->processRules();
$bt = $bt->fresh();
$this->assertEquals(BankTransaction::STATUS_MATCHED, $bt->status_id);
}
2023-02-16 02:36:09 +01:00
}