diff --git a/README.md b/README.md index 5c3d025d5ce9..f0776a9ae2bb 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ Sublime's custom image

+![phpunit](https://github.com/turbo124/invoiceninja/workflows/phpunit/badge.svg?branch=v2) [![Build Status](https://travis-ci.org/invoiceninja/invoiceninja.svg?branch=v2)](https://travis-ci.org/invoiceninja/invoiceninja) [![codecov](https://codecov.io/gh/invoiceninja/invoiceninja/branch/v2/graph/badge.svg)](https://codecov.io/gh/invoiceninja/invoiceninja) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/d39acb4bf0f74a0698dc77f382769ba5)](https://www.codacy.com/app/turbo124/invoiceninja?utm_source=github.com&utm_medium=referral&utm_content=invoiceninja/invoiceninja&utm_campaign=Badge_Grade) diff --git a/app/Http/Controllers/EmailController.php b/app/Http/Controllers/EmailController.php index c137b444caa9..277b313a9af7 100644 --- a/app/Http/Controllers/EmailController.php +++ b/app/Http/Controllers/EmailController.php @@ -115,7 +115,9 @@ class EmailController extends BaseController $entity_string = strtolower(class_basename($entity_obj)); $entity_obj->invitations->each(function ($invitation) use ($subject, $body, $entity_string, $entity_obj) { + if ($invitation->contact->send_email && $invitation->contact->email) { + $when = now()->addSeconds(1); $invitation->contact->notify((new SendGenericNotification($invitation, $entity_string, $subject, $body))->delay($when)); @@ -123,6 +125,7 @@ class EmailController extends BaseController EntitySentEmail::dispatch($invitation, $entity_string, $entity_obj->user, $invitation->company); } + }); if ($this instanceof Invoice) { diff --git a/app/Jobs/Mail/EntitySentEmail.php b/app/Jobs/Mail/EntitySentEmail.php index 8f61ac1740c0..2d6fd4fb4d07 100644 --- a/app/Jobs/Mail/EntitySentEmail.php +++ b/app/Jobs/Mail/EntitySentEmail.php @@ -5,7 +5,7 @@ namespace App\Jobs\Mail; use App\Jobs\Util\SystemLogger; use App\Libraries\Google\Google; use App\Libraries\MultiDB; -use App\Mail\Admin\EntitySent; +use App\Mail\Admin\EntityNotificationMailer; use App\Mail\Admin\EntitySentObject; use App\Models\SystemLog; use App\Models\User; @@ -56,7 +56,7 @@ class EntitySentEmail extends BaseMailerJob implements ShouldQueue */ public function handle() { - //set DB + //Set DB MultiDB::setDb($this->company->db); //if we need to set an email driver do it now @@ -66,9 +66,8 @@ class EntitySentEmail extends BaseMailerJob implements ShouldQueue $mail_obj->from = $this->entity->user->present()->name(); //send email - // Mail::to($this->user->email) - Mail::to('turbo124@gmail.com') //@todo - ->send(new EntitySent($mail_obj)); + Mail::to($this->user->email) + ->send(new EntityNotificationMailer($mail_obj)); //catch errors if (count(Mail::failures()) > 0) { diff --git a/app/Listeners/Credit/CreateCreditInvitation.php b/app/Listeners/Credit/CreateCreditInvitation.php index d9cad4755285..cafeb182869a 100644 --- a/app/Listeners/Credit/CreateCreditInvitation.php +++ b/app/Listeners/Credit/CreateCreditInvitation.php @@ -13,6 +13,7 @@ namespace App\Listeners\Credit; use App\Factory\CreditInvitationFactory; use App\Factory\InvoiceInvitationFactory; +use App\Models\CreditInvitation; use App\Models\InvoiceInvitation; use App\Utils\Traits\MakesHash; use Illuminate\Contracts\Queue\ShouldQueue; @@ -38,7 +39,7 @@ class CreateCreditInvitation implements ShouldQueue $contacts = $credit->client->contacts; $contacts->each(function ($contact) use ($credit) { - $invitation = InvoiceInvitation::whereCompanyId($credit->company_id) + $invitation = CreditInvitation::whereCompanyId($credit->company_id) ->whereClientContactId($contact->id) ->whereCreditId($credit->id) ->first(); diff --git a/app/Listeners/Quote/CreateQuoteInvitation.php b/app/Listeners/Quote/CreateQuoteInvitation.php new file mode 100644 index 000000000000..8d67a1ac7d23 --- /dev/null +++ b/app/Listeners/Quote/CreateQuoteInvitation.php @@ -0,0 +1,58 @@ +credit; + + $contacts = $quote->client->contacts; + + $contacts->each(function ($contact) use ($quote) { + $invitation = QuoteInvitation::whereCompanyId($quote->company_id) + ->whereClientContactId($contact->id) + ->whereQuoteId($quote->id) + ->first(); + + if (!$invitation && $contact->send_credit) { + $ii = QuoteInvitationFactory::create($quote->company_id, $quote->user_id); + $ii->quote_id = $quote->id; + $ii->client_contact_id = $contact->id; + $ii->save(); + } elseif ($invitation && !$contact->send_credit) { + $invitation->delete(); + } + }); + } +} diff --git a/app/Mail/Admin/EntitySent.php b/app/Mail/Admin/EntityNotificationMailer.php similarity index 95% rename from app/Mail/Admin/EntitySent.php rename to app/Mail/Admin/EntityNotificationMailer.php index c852fcfe219a..b34350e25fe2 100644 --- a/app/Mail/Admin/EntitySent.php +++ b/app/Mail/Admin/EntityNotificationMailer.php @@ -14,10 +14,11 @@ namespace App\Mail\Admin; use App\Models\User; use Illuminate\Mail\Mailable; -class EntitySent extends Mailable +class EntityNotificationMailer extends Mailable { public $mail_obj; + /** * Create a new message instance. * diff --git a/app/Mail/Admin/EntityPaidObject.php b/app/Mail/Admin/EntityPaidObject.php new file mode 100644 index 000000000000..449931c8db77 --- /dev/null +++ b/app/Mail/Admin/EntityPaidObject.php @@ -0,0 +1,96 @@ +payment = $payment; + $this->company = $payment->company; + } + + public function build() + { + $mail_obj = new \stdClass; + $mail_obj->amount = $this->getAmount(); + $mail_obj->subject = $this->getSubject(); + $mail_obj->data = $this->getData(); + $mail_obj->markdown = 'email.admin.generic'; + $mail_obj->tag = $this->company->company_key; + + return $mail_obj; + } + + private function getAmount() + { + return Number::formatMoney($this->payment->amount, $this->payment->client); + } + + private function getSubject() + { + return + ctrans( + 'texts.notification_payment_paid_subject', + ['client' => $this->payment->client->present()->name()] + ); + } + + private function getData() + { + + $settings = $this->payment->client->getMergedSettings(); + + $amount = Number::formatMoney($this->payment->amount, $this->payment->client); + + $invoice_texts = ctrans('texts.invoice_number_short'); + + foreach ($this->payment->invoices as $invoice) { + $invoice_texts .= $invoice->number . ','; + } + + $invoice_texts = substr($invoice_texts, 0, -1); + + $data = [ + 'title' => ctrans( + 'texts.notification_payment_paid_subject', + ['client' => $this->payment->client->present()->name()] + ), + 'message' => ctrans( + 'texts.notification_payment_paid', + ['amount' => $amount, + 'client' => $this->payment->client->present()->name(), + 'invoice' => $invoice_texts, + ] + ), + 'url' => config('ninja.app_url') . '/payments/' . $this->payment->hashed_id, + 'button' => ctrans('texts.view_payment'), + 'signature' => $settings->email_signature, + 'logo' => $this->company->present()->logo(), + ]; + + return $data; + } +} \ No newline at end of file diff --git a/app/Mail/Admin/EntitySentObject.php b/app/Mail/Admin/EntitySentObject.php index b1cdfe6b5620..6cb893df02e9 100644 --- a/app/Mail/Admin/EntitySentObject.php +++ b/app/Mail/Admin/EntitySentObject.php @@ -77,7 +77,6 @@ class EntitySentObject "texts.notification_{$this->entity_type}_sent", [ 'amount' => $this->getAmount(), - 'client' => $this->contact->present()->name(), 'invoice' => $this->entity->number, ] diff --git a/app/Mail/Admin/EntityViewedObject.php b/app/Mail/Admin/EntityViewedObject.php new file mode 100644 index 000000000000..ac0104cfafa1 --- /dev/null +++ b/app/Mail/Admin/EntityViewedObject.php @@ -0,0 +1,92 @@ +invitation = $invitation; + $this->entity_type = $entity_type; + $this->entity = $invitation->{$entity_type}; + $this->contact = $invitation->contact; + $this->company = $invitation->company; + } + + public function build() + { + $mail_obj = new \stdClass; + $mail_obj->amount = $this->getAmount(); + $mail_obj->subject = $this->getSubject(); + $mail_obj->data = $this->getData(); + $mail_obj->markdown = 'email.admin.generic'; + $mail_obj->tag = $this->company->company_key; + + return $mail_obj; + } + + private function getAmount() + { + return Number::formatMoney($this->entity->amount, $this->entity->client); + } + + private function getSubject() + { + return + ctrans( + "texts.notification_{$this->entity_type}_viewed_subject", + [ + 'client' => $this->contact->present()->name(), + 'invoice' => $this->entity->number, + ] + ); + } + + private function getData() + { + + $settings = $this->entity->client->getMergedSettings(); + + $data = [ + 'title' => $this->getSubject(), + 'message' => ctrans( + "texts.notification_{$this->entity_type}_viewed", + [ + 'amount' => $this->getAmount(), + 'client' => $this->contact->present()->name(), + 'invoice' => $this->entity->number, + ] + ), + 'url' => $this->invitation->getAdminLink(), + 'button' => ctrans("texts.view_{$this->entity_type}"), + 'signature' => $settings->email_signature, + 'logo' => $this->company->present()->logo(), + ]; + + return $data; + } +} \ No newline at end of file diff --git a/app/Models/Payment.php b/app/Models/Payment.php index cfc3f2a4a4d4..cac400cf7404 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -94,6 +94,11 @@ class Payment extends BaseModel return $this->belongsTo(Company::class); } + public function contact() + { + return $this->belongsTo(ClientContact::class); + } + public function user() { return $this->belongsTo(User::class)->withTrashed(); diff --git a/app/Models/PaymentType.php b/app/Models/PaymentType.php index 6f07d00eecf1..dbe86dee86ea 100644 --- a/app/Models/PaymentType.php +++ b/app/Models/PaymentType.php @@ -21,28 +21,28 @@ class PaymentType extends StaticModel public $timestamps = false; const CREDIT = 1; - const ACH = 5; - const VISA = 6; - const MASTERCARD = 7; - const AMERICAN_EXPRESS = 8; - const DISCOVER = 9; - const DINERS = 10; - const EUROCARD = 11; - const NOVA = 12; - const CREDIT_CARD_OTHER = 13; - const PAYPAL = 14; - const CARTE_BLANCHE = 17; - const UNIONPAY = 18; - const JCB = 19; - const LASER = 20; - const MAESTRO = 21; - const SOLO = 22; - const SWITCH = 23; - const ALIPAY = 28; - const SOFORT = 29; - const SEPA = 30; - const GOCARDLESS = 31; - const CRYPTO = 32; + const ACH = 4; + const VISA = 5; + const MASTERCARD = 6; + const AMERICAN_EXPRESS = 7; + const DISCOVER = 8; + const DINERS = 9; + const EUROCARD = 10; + const NOVA = 11; + const CREDIT_CARD_OTHER = 12; + const PAYPAL = 13; + const CARTE_BLANCHE = 16; + const UNIONPAY = 17; + const JCB = 18; + const LASER = 19; + const MAESTRO = 20; + const SOLO = 21; + const SWITCH = 22; + const ALIPAY = 27; + const SOFORT = 28; + const SEPA = 29; + const GOCARDLESS = 30; + const CRYPTO = 31; public static function parseCardType($cardName) { diff --git a/app/Services/Credit/MarkSent.php b/app/Services/Credit/MarkSent.php index 708819adcd3b..36cae8b51130 100644 --- a/app/Services/Credit/MarkSent.php +++ b/app/Services/Credit/MarkSent.php @@ -29,7 +29,11 @@ class MarkSent event(new CreditWasMarkedSent($this->credit, $this->credit->company)); - $this->credit->service()->setStatus(Credit::STATUS_SENT)->applyNumber()->save(); + $this->credit + ->service() + ->setStatus(Credit::STATUS_SENT) + ->applyNumber() + ->save(); return $this->credit; } diff --git a/app/Services/Quote/MarkSent.php b/app/Services/Quote/MarkSent.php index 09976a019cb3..f8f5d19bfcd5 100644 --- a/app/Services/Quote/MarkSent.php +++ b/app/Services/Quote/MarkSent.php @@ -29,7 +29,11 @@ class MarkSent event(new QuoteWasMarkedSent($this->quote, $this->quote->company)); - $this->quote->service()->setStatus(Quote::STATUS_SENT)->applyNumber()->save(); + $this->quote + ->service() + ->setStatus(Quote::STATUS_SENT) + ->applyNumber() + ->save(); return $this->quote; } diff --git a/database/factories/CreditFactory.php b/database/factories/CreditFactory.php index 57534089fb05..d45111fe2d58 100644 --- a/database/factories/CreditFactory.php +++ b/database/factories/CreditFactory.php @@ -8,7 +8,6 @@ use Faker\Generator as Faker; $factory->define(App\Models\Credit::class, function (Faker $faker) { return [ 'status_id' => App\Models\Credit::STATUS_DRAFT, - 'number' => $faker->ean13(), 'discount' => $faker->numberBetween(1, 10), 'is_amount_discount' => (bool)random_int(0, 1), 'tax_name1' => 'GST', diff --git a/database/factories/QuoteFactory.php b/database/factories/QuoteFactory.php index 04f7910414a2..c4ec77a82093 100644 --- a/database/factories/QuoteFactory.php +++ b/database/factories/QuoteFactory.php @@ -7,7 +7,6 @@ use Faker\Generator as Faker; $factory->define(App\Models\Quote::class, function (Faker $faker) { return [ 'status_id' => App\Models\Quote::STATUS_DRAFT, - 'number' => '', 'discount' => $faker->numberBetween(1, 10), 'is_amount_discount' => $faker->boolean(), 'tax_name1' => 'GST', diff --git a/database/migrations/2020_05_13_035355_add_google_refresh_token_to_users_table.php b/database/migrations/2020_05_13_035355_add_google_refresh_token_to_users_table.php index 48ecd4e30e18..09131c84e437 100644 --- a/database/migrations/2020_05_13_035355_add_google_refresh_token_to_users_table.php +++ b/database/migrations/2020_05_13_035355_add_google_refresh_token_to_users_table.php @@ -16,7 +16,6 @@ class AddGoogleRefreshTokenToUsersTable extends Migration { Schema::table('users', function (Blueprint $table) { $table->string('oauth_user_refresh_token')->nullable(); - $table->text('oauth_user_token')->change(); }); DB::statement("alter table users modify column oauth_user_token text"); diff --git a/database/seeds/RandomDataSeeder.php b/database/seeds/RandomDataSeeder.php index 20cb597eb997..27a4bc6226a4 100644 --- a/database/seeds/RandomDataSeeder.php +++ b/database/seeds/RandomDataSeeder.php @@ -21,10 +21,12 @@ use App\Models\GroupSetting; use App\Models\Invoice; use App\Models\Payment; use App\Models\PaymentType; +use App\Models\Quote; use App\Models\User; use App\Models\UserAccount; use App\Repositories\CreditRepository; use App\Repositories\InvoiceRepository; +use App\Repositories\QuoteRepository; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Schema; @@ -210,7 +212,8 @@ class RandomDataSeeder extends Seeder $credit->save(); - event(new CreateCreditInvitation($credit)); + //event(new CreateCreditInvitation($credit)); + $credit->service()->createInvitations()->markSent()->save(); //$invoice->markSent()->save(); }); @@ -220,6 +223,29 @@ class RandomDataSeeder extends Seeder // factory(\App\Models\Payment::class,20)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id, 'settings' => ClientSettings::buildClientSettings($company->settings, $client->settings)]); + /*Credits*/ + factory(\App\Models\Quote::class, 20)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); + + $quotes = Quote::cursor(); + $quote_repo = new QuoteRepository(); + + $quotes->each(function ($quote) use ($quote_repo, $user, $company, $client) { + $quote_calc = null; + + if ($quote->uses_inclusive_taxes) { + $quote_calc = new InvoiceSumInclusive($quote); + } else { + $quote_calc = new InvoiceSum($quote); + } + + $quote = $quote_calc->build()->getQuote(); + + $quote->save(); + + //event(new CreateQuoteInvitation($quote)); + $quote->service()->createInvitations()->markSent()->save(); + //$invoice->markSent()->save(); + }); $clients = Client::all(); diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 5fc427a0d2e5..01c1ee02b191 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -257,8 +257,8 @@ return [ 'notification_quote_sent_subject' => 'Quote :invoice was sent to :client', 'notification_credit_sent_subject' => 'Credit :invoice was sent to :client', 'notification_invoice_viewed_subject' => 'Invoice :invoice was viewed by :client', - 'notification_credit_viewed_subject' => 'Credit :credit was viewed by :client', - 'notification_quote_viewed_subject' => 'Quote :quote was viewed by :client', + 'notification_credit_viewed_subject' => 'Credit :invoice was viewed by :client', + 'notification_quote_viewed_subject' => 'Quote :invoice was viewed by :client', 'notification_invoice_paid' => 'A payment of :amount was made by client :client towards Invoice :invoice.', 'notification_invoice_sent' => 'The following client :client was emailed Invoice :invoice for :amount.', 'notification_quote_sent' => 'The following client :client was emailed Quote :invoice for :amount.', diff --git a/tests/Feature/PreviewTest.php b/tests/Feature/PreviewTest.php index 38a4419b7d39..2b608c0b134d 100644 --- a/tests/Feature/PreviewTest.php +++ b/tests/Feature/PreviewTest.php @@ -42,6 +42,12 @@ class PreviewTest extends TestCase $this->faker = \Faker\Factory::create(); Model::reguard(); + + + if (config('ninja.testvars.travis') !== false) { + $this->markTestSkipped('Skip test for CI Testing'); + } + } diff --git a/tests/Feature/ProductTest.php b/tests/Feature/ProductTest.php index 3be9fb3f337a..d2d973211a12 100644 --- a/tests/Feature/ProductTest.php +++ b/tests/Feature/ProductTest.php @@ -13,10 +13,11 @@ use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Http\Request; +use Illuminate\Routing\Middleware\ThrottleRequests; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Session; +use Tests\MockAccountData; use Tests\TestCase; -use Illuminate\Routing\Middleware\ThrottleRequests; /** * @test @@ -26,6 +27,7 @@ class ProductTest extends TestCase { use MakesHash; use DatabaseTransactions; + use MockAccountData; public function setUp() :void { @@ -40,42 +42,18 @@ class ProductTest extends TestCase $this->withoutMiddleware( ThrottleRequests::class ); + + $this->makeTestData(); + } public function testProductList() { - Account::all()->each(function($account) { - $account->delete(); - }); - - - $data = [ - 'first_name' => $this->faker->firstName, - 'last_name' => $this->faker->lastName, - 'name' => $this->faker->company, - 'email' => $this->faker->unique()->safeEmail, - 'password' => 'ALongAndBrilliantPassword123', - '_token' => csrf_token(), - 'privacy_policy' => 1, - 'terms_of_service' => 1 - ]; $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), - ])->post('/api/v1/signup?include=account', $data); - - $response->assertStatus(200); - - $acc = $response->json(); - - $account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id'])); - - $token = $account->default_company->tokens->first()->token; - - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, + 'X-API-TOKEN' => $this->token, ])->get('/api/v1/products'); $response->assertStatus(200); @@ -83,7 +61,7 @@ class ProductTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, + 'X-API-TOKEN' => $this->token, ])->post( '/api/v1/products/', [ @@ -112,14 +90,14 @@ class ProductTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, + 'X-API-TOKEN' => $this->token, ])->put('/api/v1/products/'.$this->encodePrimaryKey($product->id), $product_update) ->assertStatus(200); $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, + 'X-API-TOKEN' => $this->token, ])->delete('/api/v1/products/'.$this->encodePrimaryKey($product->id)) ->assertStatus(200); } diff --git a/tests/Feature/RecurringInvoiceTest.php b/tests/Feature/RecurringInvoiceTest.php index 3656ade4a467..433f6ebd82ae 100644 --- a/tests/Feature/RecurringInvoiceTest.php +++ b/tests/Feature/RecurringInvoiceTest.php @@ -15,6 +15,7 @@ use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Routing\Middleware\ThrottleRequests; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Session; +use Tests\MockAccountData; use Tests\TestCase; /** @@ -26,6 +27,7 @@ class RecurringInvoiceTest extends TestCase { use MakesHash; use DatabaseTransactions; + use MockAccountData; public function setUp() :void { @@ -40,64 +42,38 @@ class RecurringInvoiceTest extends TestCase $this->withoutMiddleware( ThrottleRequests::class ); + + $this->makeTestData(); + } public function testRecurringInvoiceList() { - $data = [ - 'first_name' => $this->faker->firstName, - 'last_name' => $this->faker->lastName, - 'name' => $this->faker->company, - 'email' => $this->faker->unique()->safeEmail, - 'password' => 'ALongAndBrilliantPassword123', - '_token' => csrf_token(), - 'privacy_policy' => 1, - 'terms_of_service' => 1 - ]; - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - ])->post('/api/v1/signup?include=account', $data); - - $acc = $response->json(); - - $account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id'])); - - $company_token = $account->default_company->tokens()->first(); - $token = $company_token->token; - $company = $company_token->company; - - $user = $company_token->user; - - $this->assertNotNull($company_token); - $this->assertNotNull($token); - $this->assertNotNull($user); - $this->assertNotNull($company); - //$this->assertNotNull($user->token->company); - - factory(\App\Models\Client::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company) { + factory(\App\Models\Client::class, 1)->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c) { factory(\App\Models\ClientContact::class, 1)->create([ - 'user_id' => $user->id, + 'user_id' => $this->user->id, 'client_id' => $c->id, - 'company_id' => $company->id, + 'company_id' => $this->company->id, 'is_primary' => 1 ]); factory(\App\Models\ClientContact::class, 1)->create([ - 'user_id' => $user->id, + 'user_id' => $this->user->id, 'client_id' => $c->id, - 'company_id' => $company->id + 'company_id' => $this->company->id ]); }); + $client = Client::all()->first(); - factory(\App\Models\RecurringInvoice::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); + factory(\App\Models\RecurringInvoice::class, 1)->create(['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $this->client->id]); $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, + 'X-API-TOKEN' => $this->token, ])->get('/api/v1/recurring_invoices'); $response->assertStatus(200); @@ -105,69 +81,39 @@ class RecurringInvoiceTest extends TestCase public function testRecurringInvoiceRESTEndPoints() { - $data = [ - 'first_name' => $this->faker->firstName, - 'last_name' => $this->faker->lastName, - 'name' => $this->faker->company, - 'email' => $this->faker->unique()->safeEmail, - 'password' => 'ALongAndBrilliantPassword123', - '_token' => csrf_token(), - 'privacy_policy' => 1, - 'terms_of_service' => 1 - ]; - - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - ])->post('/api/v1/signup?include=account', $data); - - $acc = $response->json(); - $account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id'])); - - $company_token = $account->default_company->tokens()->first(); - $token = $company_token->token; - $company = $company_token->company; - - $user = $company_token->user; - - $this->assertNotNull($company_token); - $this->assertNotNull($token); - $this->assertNotNull($user); - $this->assertNotNull($company); - //$this->assertNotNull($user->token->company); - - factory(\App\Models\Client::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company) { + factory(\App\Models\Client::class, 1)->create(['user_id' => $this->user->id, 'company_id' => $this->company->id])->each(function ($c) { factory(\App\Models\ClientContact::class, 1)->create([ - 'user_id' => $user->id, + 'user_id' => $this->user->id, 'client_id' => $c->id, - 'company_id' => $company->id, + 'company_id' => $this->company->id, 'is_primary' => 1 ]); factory(\App\Models\ClientContact::class, 1)->create([ - 'user_id' => $user->id, + 'user_id' => $this->user->id, 'client_id' => $c->id, - 'company_id' => $company->id + 'company_id' => $this->company->id ]); }); $client = Client::all()->first(); - factory(\App\Models\RecurringInvoice::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); + factory(\App\Models\RecurringInvoice::class, 1)->create(['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $this->client->id]); - $RecurringInvoice = RecurringInvoice::where('user_id', $user->id)->first(); + $RecurringInvoice = RecurringInvoice::where('user_id', $this->user->id)->first(); $RecurringInvoice->save(); $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, + 'X-API-TOKEN' => $this->token, ])->get('/api/v1/recurring_invoices/'.$this->encodePrimaryKey($RecurringInvoice->id)); $response->assertStatus(200); $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, + 'X-API-TOKEN' => $this->token, ])->get('/api/v1/recurring_invoices/'.$this->encodePrimaryKey($RecurringInvoice->id).'/edit'); $response->assertStatus(200); @@ -181,13 +127,13 @@ class RecurringInvoiceTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, + 'X-API-TOKEN' => $this->token, ])->put('/api/v1/recurring_invoices/'.$this->encodePrimaryKey($RecurringInvoice->id), $RecurringInvoice_update) ->assertStatus(200); $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, + 'X-API-TOKEN' => $this->token, ])->delete('/api/v1/recurring_invoices/'.$this->encodePrimaryKey($RecurringInvoice->id)); $response->assertStatus(200); diff --git a/tests/Feature/RecurringQuoteTest.php b/tests/Feature/RecurringQuoteTest.php index 2a984e29fc6a..db4e0cf2a291 100644 --- a/tests/Feature/RecurringQuoteTest.php +++ b/tests/Feature/RecurringQuoteTest.php @@ -15,6 +15,7 @@ use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Routing\Middleware\ThrottleRequests; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Session; +use Tests\MockAccountData; use Tests\TestCase; /** @@ -26,6 +27,7 @@ class RecurringQuoteTest extends TestCase { use MakesHash; use DatabaseTransactions; + use MockAccountData; public function setUp() :void { @@ -40,64 +42,20 @@ class RecurringQuoteTest extends TestCase $this->withoutMiddleware( ThrottleRequests::class ); + + $this->makeTestData(); + } public function testRecurringQuoteList() { - $data = [ - 'first_name' => $this->faker->firstName, - 'last_name' => $this->faker->lastName, - 'name' => $this->faker->company, - 'email' => $this->faker->unique()->safeEmail, - 'password' => 'ALongAndBrilliantPassword123', - '_token' => csrf_token(), - 'privacy_policy' => 1, - 'terms_of_service' => 1 - ]; + + factory(\App\Models\RecurringQuote::class, 1)->create(['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $this->client->id]); $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), - ])->post('/api/v1/signup?include=account', $data); - - $acc = $response->json(); - - $account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id'])); - - $company_token = $account->default_company->tokens()->first(); - $token = $company_token->token; - $company = $company_token->company; - - $user = $company_token->user; - - $this->assertNotNull($company_token); - $this->assertNotNull($token); - $this->assertNotNull($user); - $this->assertNotNull($company); - //$this->assertNotNull($user->token->company); - - factory(\App\Models\Client::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company) { - factory(\App\Models\ClientContact::class, 1)->create([ - 'user_id' => $user->id, - 'client_id' => $c->id, - 'company_id' => $company->id, - 'is_primary' => 1 - ]); - - factory(\App\Models\ClientContact::class, 1)->create([ - 'user_id' => $user->id, - 'client_id' => $c->id, - 'company_id' => $company->id - ]); - }); - $client = Client::all()->first(); - - factory(\App\Models\RecurringQuote::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); - - - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, + 'X-API-TOKEN' => $this->token, ])->get('/api/v1/recurring_quotes'); $response->assertStatus(200); @@ -105,70 +63,24 @@ class RecurringQuoteTest extends TestCase public function testRecurringQuoteRESTEndPoints() { - $data = [ - 'first_name' => $this->faker->firstName, - 'last_name' => $this->faker->lastName, - 'name' => $this->faker->company, - 'email' => $this->faker->unique()->safeEmail, - 'password' => 'ALongAndBrilliantPassword123', - '_token' => csrf_token(), - 'privacy_policy' => 1, - 'terms_of_service' => 1 - ]; - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - ])->post('/api/v1/signup?include=account', $data); + factory(\App\Models\RecurringQuote::class, 1)->create(['user_id' => $this->user->id, 'company_id' => $this->company->id, 'client_id' => $this->client->id]); - $acc = $response->json(); - - $account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id'])); - - $company_token = $account->default_company->tokens()->first(); - $token = $company_token->token; - $company = $company_token->company; - - $user = $company_token->user; - - $this->assertNotNull($company_token); - $this->assertNotNull($token); - $this->assertNotNull($user); - $this->assertNotNull($company); - //$this->assertNotNull($user->token->company); - - factory(\App\Models\Client::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company) { - factory(\App\Models\ClientContact::class, 1)->create([ - 'user_id' => $user->id, - 'client_id' => $c->id, - 'company_id' => $company->id, - 'is_primary' => 1 - ]); - - factory(\App\Models\ClientContact::class, 1)->create([ - 'user_id' => $user->id, - 'client_id' => $c->id, - 'company_id' => $company->id - ]); - }); - $client = Client::all()->first(); - - factory(\App\Models\RecurringQuote::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); - - $RecurringQuote = RecurringQuote::where('user_id', $user->id)->first(); + $RecurringQuote = RecurringQuote::where('user_id', $this->user->id)->first(); $RecurringQuote->save(); $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, + 'X-API-TOKEN' => $this->token, ])->get('/api/v1/recurring_quotes/'.$this->encodePrimaryKey($RecurringQuote->id)); $response->assertStatus(200); $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, + 'X-API-TOKEN' => $this->token, ])->get('/api/v1/recurring_quotes/'.$this->encodePrimaryKey($RecurringQuote->id).'/edit'); $response->assertStatus(200); @@ -183,13 +95,13 @@ class RecurringQuoteTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, + 'X-API-TOKEN' => $this->token, ])->put('/api/v1/recurring_quotes/'.$this->encodePrimaryKey($RecurringQuote->id), $RecurringQuote_update) ->assertStatus(200); $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $token, + 'X-API-TOKEN' => $this->token, ])->delete('/api/v1/recurring_quotes/'.$this->encodePrimaryKey($RecurringQuote->id)); $response->assertStatus(200);