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

325 lines
9.5 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;
2020-10-28 07:27:10 +01:00
use App\Jobs\Entity\EmailEntity;
2023-11-26 08:41:42 +01:00
use App\Models\SystemLog;
use App\Utils\Traits\GeneratesCounter;
use Illuminate\Database\Eloquent\Model;
2023-11-26 08:41:42 +01:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
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-11-26 08:41:42 +01:00
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
* @covers App\Jobs\Invoice\EmailInvoice
*/
class InvoiceEmailTest extends TestCase
{
use MockAccountData;
use DatabaseTransactions;
use GeneratesCounter;
2023-10-18 06:38:45 +02:00
public $faker;
2024-08-22 08:57:52 +02:00
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();
}
public function testInvalidEmailParsing()
{
$email = 'illegal@example.com';
$this->assertTrue(strpos($email, '@example.com') !== false);
}
2024-08-22 08:57:52 +02:00
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,
2024-08-12 06:30:19 +02:00
'entity' => 'invoice',
2023-08-23 09:13:46 +02:00
'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') ?? '',
],
],
]
];
2024-08-22 08:57:52 +02:00
2023-08-23 09:13:46 +02:00
$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();
2024-08-12 06:30:19 +02:00
$this->assertEquals('invoice', $arr[0]['entity']);
2023-08-23 09:13:46 +02:00
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();
2023-11-26 08:41:42 +01:00
$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,
2024-08-12 06:30:19 +02:00
'entity' => 'invoice',
2023-08-23 09:13:46 +02:00
'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') ?? '',
],
],
]
];
2024-08-22 08:57:52 +02:00
2023-08-23 09:13:46 +02:00
$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();
2024-08-22 08:45:06 +02:00
2024-08-12 06:30:19 +02:00
$this->assertEquals('invoice', $arr[0]['entity']);
2023-08-23 09:13:46 +02:00
$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',
2024-08-22 08:57:52 +02:00
"entity_id" => $this->invoice->hashed_id,
"subject" => 'Reminder $number',
"template" => "first_custom"
2023-08-05 01:31:16 +02:00
];
$response = false;
2024-08-22 08:57:52 +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 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;
2024-08-22 08:57:52 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson('/api/v1/emails', $data);
2024-08-22 08:45:06 +02:00
$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);
2024-08-22 08:57:52 +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);
2024-08-22 08:57:52 +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);
2024-08-22 08:57:52 +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
}