Tests for entity history

This commit is contained in:
David Bomba 2023-08-23 17:13:46 +10:00
parent 2f1968c899
commit 21f1469499
2 changed files with 100 additions and 5 deletions

View File

@ -30,9 +30,11 @@ class EmailHistoryController extends BaseController
$data = SystemLog::where('client_id', $client->id)
->where('category_id', SystemLog::CATEGORY_MAIL)
->orderBy('id', 'DESC')
->cursor()
->map(function ($system_log) {
if($system_log->log['history'] ?? false) {
return json_decode($system_log->log['history'], true);
return $system_log->log['history'];
// return json_decode($system_log->log['history'], true);
}
});
@ -54,9 +56,11 @@ class EmailHistoryController extends BaseController
->where('category_id', SystemLog::CATEGORY_MAIL)
->whereJsonContains('log->history->entity_id', $this->encodePrimaryKey($request->entity_id))
->orderBy('id', 'DESC')
->cursor()
->map(function ($system_log) {
if($system_log->log['history'] ?? false) {
return json_decode($system_log->log['history'], true);
return $system_log->log['history'];
// return json_decode($system_log->log['history'], true);
}
});

View File

@ -11,14 +11,15 @@
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\SystemLog;
use Tests\MockAccountData;
use App\Jobs\Entity\EmailEntity;
use App\Utils\Traits\GeneratesCounter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Session;
use Tests\MockAccountData;
use Tests\TestCase;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
/**
* @test
@ -46,6 +47,96 @@ class InvoiceEmailTest extends TestCase
}
public function testClientEmailHistory()
{
$system_log = new SystemLog();
$system_log->company_id = $this->company->id;
$system_log->client_id = $this->client->id;
$system_log->category_id = SystemLog::CATEGORY_MAIL;
$system_log->event_id = SystemLog::EVENT_MAIL_SEND;
$system_log->type_id = SystemLog::TYPE_WEBHOOK_RESPONSE;
$system_log->log = [
'history' => [
'entity_id' => $this->invoice->hashed_id,
'entity_type' => 'invoice',
'subject' => 'Invoice #1',
'events' => [
[
'recipient' => 'bob@gmail.com',
'status' => 'Delivered',
'delivery_message' => 'A message that was deliveryed',
'server' => 'email.mx.com',
'server_ip' => '127.0.0.1',
'date' => \Carbon\Carbon::parse('2023-10-10')->format('Y-m-d H:m:s') ?? '',
],
],
]
];
$system_log->save();
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson('/api/v1/emails/clientHistory/'.$this->client->hashed_id);
$response->assertStatus(200);
$arr = $response->json();
$this->assertEquals('invoice', $arr[0]['entity_type']);
}
public function testEntityEmailHistory()
{
$system_log = new SystemLog();
$system_log->company_id = $this->company->id;
$system_log->client_id = $this->client->id;
$system_log->category_id = SystemLog::CATEGORY_MAIL;
$system_log->event_id = SystemLog::EVENT_MAIL_SEND;
$system_log->type_id = SystemLog::TYPE_WEBHOOK_RESPONSE;
$system_log->log = [
'history' => [
'entity_id' => $this->invoice->hashed_id,
'entity_type' => 'invoice',
'subject' => 'Invoice #1',
'events' => [
[
'recipient' => 'bob@gmail.com',
'status' => 'Delivered',
'delivery_message' => 'A message that was deliveryed',
'server' => 'email.mx.com',
'server_ip' => '127.0.0.1',
'date' => \Carbon\Carbon::parse('2023-10-10')->format('Y-m-d H:m:s') ?? '',
],
],
]
];
$system_log->save();
$data = [
'entity' => 'invoice',
'entity_id' => $this->invoice->hashed_id,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson('/api/v1/emails/entityHistory/', $data);
$response->assertStatus(200);
$arr = $response->json();
$this->assertEquals('invoice', $arr[0]['entity_type']);
$this->assertEquals($this->invoice->hashed_id, $arr[0]['entity_id']);
}
public function testTemplateValidation()
{
$data = [