1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/tests/Feature/InvoiceEmailTest.php

323 lines
9.8 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://www.elastic.co/licensing/elastic-license
2020-09-14 13:11:46 +02:00
*/
2020-10-28 11:10:49 +01:00
namespace Tests\Feature;
2023-08-23 09:13:46 +02:00
use Tests\TestCase;
use App\Models\SystemLog;
use Tests\MockAccountData;
2020-10-28 07:27:10 +01:00
use App\Jobs\Entity\EmailEntity;
use App\Utils\Traits\GeneratesCounter;
use Illuminate\Database\Eloquent\Model;
2023-08-21 08:43:03 +02:00
use Illuminate\Support\Facades\Bus;
2023-08-21 11:31:25 +02:00
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException;
2023-08-23 09:13:46 +02:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
/**
* @test
* @covers App\Jobs\Invoice\EmailInvoice
*/
class InvoiceEmailTest extends TestCase
{
use MockAccountData;
use DatabaseTransactions;
use GeneratesCounter;
protected function setUp() :void
{
parent::setUp();
Session::start();
$this->faker = \Faker\Factory::create();
Model::reguard();
$this->makeTestData();
2023-08-05 02:01:44 +02:00
// $this->withoutExceptionHandling();
}
2023-08-23 09:13:46 +02:00
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']);
2023-08-23 09:36:39 +02:00
$count = SystemLog::where('client_id', $this->client->id)
->where('category_id', SystemLog::CATEGORY_MAIL)
->orderBy('id', 'DESC')
->count();
$this->assertEquals(1, $count);
2023-08-23 09:13:46 +02:00
}
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']);
2023-08-23 09:36:39 +02:00
$count = SystemLog::where('company_id', $this->company->id)
->where('category_id', SystemLog::CATEGORY_MAIL)
->whereJsonContains('log->history->entity_id', $this->invoice->hashed_id)
->count();
$this->assertEquals(1, $count);
2023-08-23 09:13:46 +02:00
}
2023-08-05 01:31:16 +02:00
public function testTemplateValidation()
{
$data = [
2023-08-21 11:31:25 +02:00
"body" => "hey what's up",
"entity" => 'invoice',
"entity_id"=> $this->invoice->hashed_id,
"subject"=> 'Reminder $number',
2023-08-05 01:31:16 +02:00
"template"=> "first_custom"
];
$response = false;
2023-08-05 02:01:44 +02:00
try {
2023-08-05 01:31:16 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson('/api/v1/emails', $data);
2023-08-05 02:01:44 +02:00
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
nlog($message);
}
2023-08-05 01:31:16 +02:00
2023-08-05 02:01:44 +02:00
$response->assertStatus(422);
2023-08-05 01:31:16 +02:00
}
public function test_cc_email_implementation()
{
$data = [
'template' => 'email_template_invoice',
'entity' => 'invoice',
'entity_id' => $this->invoice->hashed_id,
'cc_email' => 'jj@gmail.com'
];
$response = false;
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson('/api/v1/emails', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
nlog($message);
}
$response->assertStatus(200);
}
public function test_initial_email_send_emails()
{
$this->invoice->date = now();
$this->invoice->due_date = now()->addDays(7);
2020-10-07 01:16:57 +02:00
$this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice);
2020-02-15 12:49:31 +01:00
$this->invoice->client_id = $this->client->id;
$client_settings = $this->client->settings;
$client_settings->email_style = 'dark';
$this->client->settings = $client_settings;
$this->client->save();
2020-02-15 12:49:31 +01:00
$this->invoice->setRelation('client', $this->client);
$this->invoice->save();
2023-08-21 08:43:03 +02:00
Bus::fake();
$this->invoice->invitations->each(function ($invitation) {
if ($invitation->contact->send_email && $invitation->contact->email) {
2022-06-24 13:33:43 +02:00
EmailEntity::dispatch($invitation, $invitation->company);
2023-08-21 08:43:03 +02:00
Bus::assertDispatched(EmailEntity::class);
2020-02-15 12:49:31 +01:00
}
});
}
public function testTemplateThemes()
{
$settings = $this->company->settings;
$settings->email_style = 'light';
$this->company->settings = $settings;
$this->company->save();
$this->invoice->date = now();
$this->invoice->due_date = now()->addDays(7);
2020-10-07 01:16:57 +02:00
$this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice);
$this->invoice->client_id = $this->client->id;
$this->invoice->setRelation('client', $this->client);
$this->invoice->save();
2023-08-21 08:43:03 +02:00
Bus::fake();
$this->invoice->invitations->each(function ($invitation) {
if ($invitation->contact->send_email && $invitation->contact->email) {
2022-06-24 13:33:43 +02:00
EmailEntity::dispatch($invitation, $invitation->company);
2023-08-21 07:25:46 +02:00
2023-08-21 11:31:25 +02:00
Bus::assertDispatched(EmailEntity::class);
2023-08-21 07:25:46 +02:00
}
});
$settings = $this->company->settings;
$settings->email_style = 'dark';
$this->company->settings = $settings;
$this->company->save();
$this->invoice->date = now();
$this->invoice->due_date = now()->addDays(7);
2020-10-07 01:16:57 +02:00
$this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice);
$this->invoice->client_id = $this->client->id;
$client_settings = $this->client->settings;
$client_settings->email_style = 'dark';
$this->client->settings = $client_settings;
$this->client->save();
$this->invoice->setRelation('client', $this->client);
$this->invoice->save();
2023-08-21 08:43:03 +02:00
Bus::fake();
$this->invoice->invitations->each(function ($invitation) {
if ($invitation->contact->send_email && $invitation->contact->email) {
2022-06-24 13:33:43 +02:00
EmailEntity::dispatch($invitation, $invitation->company);
2023-08-21 07:25:46 +02:00
2023-08-21 11:31:25 +02:00
Bus::assertDispatched(EmailEntity::class);
2023-08-21 07:25:46 +02:00
}
});
$settings = $this->company->settings;
$settings->email_style = 'plain';
$this->company->settings = $settings;
$this->company->save();
$this->invoice->date = now();
$this->invoice->due_date = now()->addDays(7);
2020-10-07 01:16:57 +02:00
$this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice);
$this->invoice->client_id = $this->client->id;
$this->invoice->setRelation('client', $this->client);
$this->invoice->save();
2023-08-21 08:43:03 +02:00
Bus::fake();
$this->invoice->invitations->each(function ($invitation) {
if ($invitation->contact->send_email && $invitation->contact->email) {
2022-06-24 13:33:43 +02:00
EmailEntity::dispatch($invitation, $invitation->company);
2023-08-21 07:25:46 +02:00
2023-08-21 11:31:25 +02:00
Bus::assertDispatched(EmailEntity::class);
2023-08-21 07:25:46 +02:00
}
});
$this->assertTrue(true);
}
2020-02-15 12:49:31 +01:00
}