fix: 🐛 change the customer's opt-in value if it exists when call api optin route #27
This commit is contained in:
parent
06694f0a48
commit
0f008b0db4
4 changed files with 210 additions and 108 deletions
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
namespace App\Http\Controllers\Api\V4;
|
||||
|
||||
use App\Enums\ApiError;
|
||||
use App\Exceptions\Api\V4\ApiErrorException;
|
||||
use App\Exceptions\Api\V4\CrmException;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\V4\BaseApiRequest;
|
||||
|
|
@ -21,34 +19,49 @@ class OptinController extends Controller
|
|||
* @param \App\Http\Requests\Api\V4\SingleOptinRequest $request
|
||||
* @param \App\Models\Service $service
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @throws \App\Exceptions\Api\V4\ApiErrorException With ApiError::singleOptinCustomerExists
|
||||
* if this email already has a full account.
|
||||
* @throws \App\Exceptions\Api\V4\CrmException If the single optin could not be added or removed.
|
||||
*/
|
||||
public function optin(SingleOptinRequest $request, Service $service): \Illuminate\Http\JsonResponse
|
||||
{
|
||||
$validatedData = $request->validated();
|
||||
if (CustomerInfo::getFromEmail($service->brand, $validatedData['email'])->exists()) {
|
||||
throw new ApiErrorException(ApiError::singleOptinCustomerExists);
|
||||
}
|
||||
$crmCustomer = CustomerInfo::getFromEmail($service->brand, $validatedData['email'])->first();
|
||||
if ($crmCustomer) {
|
||||
// Update the optin field.
|
||||
$crmCustomer->opt_in = \boolval($validatedData['opt_in']);
|
||||
|
||||
/** @var \App\Models\SingleOptin */
|
||||
$singleOptin = SingleOptin::query()
|
||||
->where('email', $validatedData['email'])
|
||||
->whereHas('brand', function ($q) use ($service) {
|
||||
$q->whereKey($service->brand->getKey());
|
||||
})
|
||||
->firstOrNew();
|
||||
// Check the saving.
|
||||
if (!$crmCustomer->saveOrFail()) {
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new CrmException('Api failed to update customer optin.');
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
if (!$this->changeOptin($service, $request, \boolval($validatedData['opt_in']), $singleOptin)) {
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new CrmException('Api failed to update single optin.');
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
// Associate answers if presents.
|
||||
if (isset($validatedData['answer_id'])) {
|
||||
$crmCustomer->answers()->attach($validatedData['answer_id']);
|
||||
}
|
||||
} else {
|
||||
// Get the single optin if exist or create it.
|
||||
/** @var \App\Models\SingleOptin */
|
||||
$singleOptin = SingleOptin::query()
|
||||
->where('email', $validatedData['email'])
|
||||
->whereHas('brand', function ($q) use ($service) {
|
||||
$q->whereKey($service->brand->getKey());
|
||||
})
|
||||
->firstOrNew();
|
||||
|
||||
if (isset($validatedData['answer_id'])) {
|
||||
$singleOptin->answers()->attach($validatedData['answer_id']);
|
||||
}
|
||||
// Update the optin field and check the saving.
|
||||
if (!$this->changeOptin($service, $request, \boolval($validatedData['opt_in']), $singleOptin)) {
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new CrmException('Api failed to update single optin.');
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
// Associate answers if presents.
|
||||
if (isset($validatedData['answer_id'])) {
|
||||
$singleOptin->answers()->attach($validatedData['answer_id']);
|
||||
}
|
||||
} //end if
|
||||
|
||||
return response()->json();
|
||||
}
|
||||
|
|
@ -65,17 +78,33 @@ class OptinController extends Controller
|
|||
SingleOptinRemoveWithHashcodeRequest $request,
|
||||
Service $service
|
||||
): \Illuminate\Http\JsonResponse {
|
||||
/** @var \App\Models\SingleOptin */
|
||||
$singleOptin = SingleOptin::query()->where('hashcode', $request->hashcode)
|
||||
->whereHas('brand', function ($q) use ($service) {
|
||||
$q->whereId($service->brand->id);
|
||||
})->firstOrFail();
|
||||
$validatedData = $request->validated();
|
||||
$crmCustomer = CustomerInfo::getFromHashcode($service->brand, $validatedData['hashcode'])->first();
|
||||
if ($crmCustomer) {
|
||||
// Update the optin field.
|
||||
$crmCustomer->opt_in = false;
|
||||
|
||||
if ($singleOptin and !$this->changeOptin($service, $request, \boolval($request->opt_in), $singleOptin)) {
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new CrmException('Api failed to update single optin.');
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
// Check the saving.
|
||||
if (!$crmCustomer->saveOrFail()) {
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new CrmException('Api failed to update customer optin.');
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
} else {
|
||||
// Get the single optin if exist or create it.
|
||||
/** @var \App\Models\SingleOptin */
|
||||
$singleOptin = SingleOptin::query()->where('hashcode', $validatedData['hashcode'])
|
||||
->whereHas('brand', function ($q) use ($service) {
|
||||
$q->whereId($service->brand->getKey());
|
||||
})->firstOrFail();
|
||||
|
||||
// Update the optin field and check the saving.
|
||||
if ($singleOptin and !$this->changeOptin($service, $request, false, $singleOptin)) {
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new CrmException('Api failed to update single optin.');
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
} //end if
|
||||
|
||||
return response()->json();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,9 @@ class SingleOptinTest extends ApiBase
|
|||
use CreatesApplication;
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @var string $email */
|
||||
private $email = 'singleoptin@hotmail.fr';
|
||||
|
||||
/**
|
||||
* Test set single optin on.
|
||||
* route: apiV2.optin
|
||||
|
|
@ -28,13 +31,13 @@ class SingleOptinTest extends ApiBase
|
|||
$service = Service::firstOrFail();
|
||||
|
||||
$response = $this->callApiWithToken($service, 'post', route('apiV2.optin'), [
|
||||
'email' => 'singleoptin@hotmail.fr',
|
||||
'opt_in' => 1
|
||||
'email' => $this->email,
|
||||
'opt_in' => 1,
|
||||
]);
|
||||
$json = $response->decodeResponseJson();
|
||||
$this->assertApiResponseStatusIs($json, ApiStatus::OK);
|
||||
$this->assertTrue(
|
||||
SingleOptin::query()->where('email', 'singleoptin@hotmail.fr')->exists(),
|
||||
SingleOptin::query()->where('email', $this->email)->exists(),
|
||||
'Single optin should have been set for email "singleoptin@hotmail.fr"'
|
||||
);
|
||||
}
|
||||
|
|
@ -58,12 +61,12 @@ class SingleOptinTest extends ApiBase
|
|||
$firstAnswer = $questions->first()->answers->first();
|
||||
|
||||
$response = $this->callApiWithToken($service, 'post', route('apiV2.optin'), [
|
||||
'email' => 'singleoptin@hotmail.fr',
|
||||
'email' => $this->email,
|
||||
'opt_in' => 1,
|
||||
'answer_id' => [$firstAnswer->getkey()],
|
||||
]);
|
||||
|
||||
$singleOptin = SingleOptin::query()->where('email', 'singleoptin@hotmail.fr');
|
||||
$singleOptin = SingleOptin::query()->where('email', $this->email);
|
||||
$json = $response->decodeResponseJson();
|
||||
$this->assertApiResponseStatusIs($json, ApiStatus::OK);
|
||||
$this->assertTrue(
|
||||
|
|
@ -87,13 +90,13 @@ class SingleOptinTest extends ApiBase
|
|||
$service = Service::firstOrFail();
|
||||
|
||||
$response = $this->callApiWithToken($service, 'post', route('apiV2.optin'), [
|
||||
'email' => 'singleoptin@hotmail.fr',
|
||||
'opt_in' => 0
|
||||
'email' => $this->email,
|
||||
'opt_in' => 0,
|
||||
]);
|
||||
$json = $response->decodeResponseJson();
|
||||
$this->assertApiResponseStatusIs($json, ApiStatus::OK);
|
||||
$this->assertFalse(
|
||||
SingleOptin::query()->where('email', 'singleoptin@hotmail.fr')->exists(),
|
||||
SingleOptin::query()->where('email', $this->email)->exists(),
|
||||
'Single optin should have been removed for email "singleoptin@hotmail.fr"'
|
||||
);
|
||||
}
|
||||
|
|
@ -104,20 +107,20 @@ class SingleOptinTest extends ApiBase
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSingleOptinShouldFailIfCustomerExists(): void
|
||||
public function testSingleOptinShouldFailIfOptinAlreadyExists(): void
|
||||
{
|
||||
$service = Service::firstOrFail();
|
||||
$this->storeCustomer($service);
|
||||
|
||||
// * Make sure a single optin exists.
|
||||
$singleOptin = SingleOptin::query()->where('email', 'singleoptin@hotmail.fr')->firstOrNew();
|
||||
$singleOptin = SingleOptin::query()->where('email', $this->email)->firstOrNew();
|
||||
$singleOptin->brand()->associate($service->brand);
|
||||
$singleOptin->email = 'singleoptin@hotmail.fr';
|
||||
$singleOptin->email = $this->email;
|
||||
$singleOptin->saveOrFail();
|
||||
|
||||
$response = $this->callApiWithToken($service, 'post', route('apiV2.optin'), [
|
||||
'email' => 'singleoptin@hotmail.fr',
|
||||
'opt_in' => 1
|
||||
'email' => $this->email,
|
||||
'opt_in' => 1,
|
||||
]);
|
||||
|
||||
$json = $response->decodeResponseJson();
|
||||
|
|
@ -135,7 +138,7 @@ class SingleOptinTest extends ApiBase
|
|||
{
|
||||
$service = Service::query()->firstOrFail();
|
||||
$response = $this->callApiWithToken($service, 'post', route('apiV2.optin'), [
|
||||
'email' => 'singleoptin@hotmail.fr',
|
||||
'email' => $this->email,
|
||||
'opt_in' => 1,
|
||||
'answer_id' => [0],
|
||||
]);
|
||||
|
|
@ -163,7 +166,7 @@ class SingleOptinTest extends ApiBase
|
|||
$singleOptin->refresh();
|
||||
|
||||
$response = $this->callApiWithToken($service, 'post', route('apiV2.optout-hashcode'), [
|
||||
'hashcode' => $singleOptin->hashcode
|
||||
'hashcode' => $singleOptin->hashcode,
|
||||
]);
|
||||
$json = $response->decodeResponseJson();
|
||||
$this->assertApiResponseStatusIs($json, ApiStatus::OK);
|
||||
|
|
@ -194,7 +197,7 @@ class SingleOptinTest extends ApiBase
|
|||
$singleOptin->delete();
|
||||
|
||||
$response = $this->callApiWithToken($service, 'post', route('apiV2.optout-hashcode'), [
|
||||
'hashcode' => $singleOptin->hashcode
|
||||
'hashcode' => $singleOptin->hashcode,
|
||||
]);
|
||||
$json = $response->decodeResponseJson();
|
||||
$this->assertApiResponseStatusIs($json, ApiStatus::KO);
|
||||
|
|
@ -214,7 +217,7 @@ class SingleOptinTest extends ApiBase
|
|||
'lastname' => 'Bond',
|
||||
'birth_date' => \date('1992-03-08'),
|
||||
'address' => '10 rue des totos',
|
||||
'email' => 'singleoptin@hotmail.fr',
|
||||
'email' => $this->email,
|
||||
'password' => '123.Mot.De.P4sse',
|
||||
'phone' => '+33654821562',
|
||||
'country' => Country::france->value(),
|
||||
|
|
|
|||
|
|
@ -5,10 +5,12 @@ namespace Tests\CRM\ApiV4;
|
|||
use App\Enums\ApiError;
|
||||
use App\Enums\ApiStatus;
|
||||
use App\Enums\Country;
|
||||
use App\Models\CustomerInfo;
|
||||
use App\Models\ModelAnswer;
|
||||
use App\Models\Question;
|
||||
use App\Models\Service;
|
||||
use App\Models\SingleOptin;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Tests\CreatesApplication;
|
||||
|
||||
|
|
@ -17,6 +19,9 @@ class SingleOptinTest extends ApiBase
|
|||
use CreatesApplication;
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @var string $email */
|
||||
private $email = 'singleoptin@hotmail.fr';
|
||||
|
||||
/**
|
||||
* Test set single optin on.
|
||||
* route: apiV4.optin
|
||||
|
|
@ -28,13 +33,13 @@ class SingleOptinTest extends ApiBase
|
|||
$service = Service::firstOrFail();
|
||||
|
||||
$response = $this->callApiWithToken($service, 'post', route('apiV4.optin'), [
|
||||
'email' => 'singleoptin@hotmail.fr',
|
||||
'opt_in' => 1
|
||||
'email' => $this->email,
|
||||
'opt_in' => 1,
|
||||
]);
|
||||
$json = $response->decodeResponseJson();
|
||||
$this->assertApiResponseStatusIs($json, ApiStatus::OK);
|
||||
$this->assertTrue(
|
||||
SingleOptin::query()->where('email', 'singleoptin@hotmail.fr')->exists(),
|
||||
SingleOptin::query()->where('email', $this->email)->exists(),
|
||||
'Single optin should have been set for email "singleoptin@hotmail.fr"'
|
||||
);
|
||||
}
|
||||
|
|
@ -58,12 +63,12 @@ class SingleOptinTest extends ApiBase
|
|||
$firstAnswer = $questions->first()->answers->first();
|
||||
|
||||
$response = $this->callApiWithToken($service, 'post', route('apiV4.optin'), [
|
||||
'email' => 'singleoptin@hotmail.fr',
|
||||
'email' => $this->email,
|
||||
'opt_in' => 1,
|
||||
'answer_id' => [$firstAnswer->getkey()],
|
||||
]);
|
||||
|
||||
$singleOptin = SingleOptin::query()->where('email', 'singleoptin@hotmail.fr');
|
||||
$singleOptin = SingleOptin::query()->where('email', $this->email);
|
||||
$json = $response->decodeResponseJson();
|
||||
$this->assertApiResponseStatusIs($json, ApiStatus::OK);
|
||||
$this->assertTrue(
|
||||
|
|
@ -87,43 +92,42 @@ class SingleOptinTest extends ApiBase
|
|||
$service = Service::firstOrFail();
|
||||
|
||||
$response = $this->callApiWithToken($service, 'post', route('apiV4.optin'), [
|
||||
'email' => 'singleoptin@hotmail.fr',
|
||||
'opt_in' => 0
|
||||
'email' => $this->email,
|
||||
'opt_in' => 0,
|
||||
]);
|
||||
$json = $response->decodeResponseJson();
|
||||
$this->assertApiResponseStatusIs($json, ApiStatus::OK);
|
||||
$this->assertFalse(
|
||||
SingleOptin::query()->where('email', 'singleoptin@hotmail.fr')->exists(),
|
||||
SingleOptin::query()->where('email', $this->email)->exists(),
|
||||
'Single optin should have been removed for email "singleoptin@hotmail.fr"'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should throw an ApiError exception if trying to set
|
||||
* optin to an email that already have an account.
|
||||
* Test set single optin on when customer exist.
|
||||
* route: apiV4.optin
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSingleOptinShouldFailIfCustomerExists(): void
|
||||
public function testSetSingleOptinWhenCustomerExist(): void
|
||||
{
|
||||
$service = Service::firstOrFail();
|
||||
$this->storeCustomer($service);
|
||||
|
||||
// * Make sure a single optin exists.
|
||||
$singleOptin = SingleOptin::query()->where('email', 'singleoptin@hotmail.fr')->firstOrNew();
|
||||
$singleOptin->brand()->associate($service->brand);
|
||||
$singleOptin->email = 'singleoptin@hotmail.fr';
|
||||
$singleOptin->saveOrFail();
|
||||
|
||||
$response = $this->callApiWithToken($service, 'post', route('apiV4.optin'), [
|
||||
'email' => 'singleoptin@hotmail.fr',
|
||||
'opt_in' => 1
|
||||
'email' => $this->email,
|
||||
'opt_in' => 1,
|
||||
]);
|
||||
|
||||
$json = $response->decodeResponseJson();
|
||||
$this->assertApiResponseStatusIs($json, ApiStatus::KO);
|
||||
$this->assertEquals(ApiError::singleOptinCustomerExists->value, $json->json('data.code'));
|
||||
$this->assertEquals(ApiError::singleOptinCustomerExists->label(), $json->json('data.message'));
|
||||
$json = $response->decodeResponseJson();
|
||||
$this->assertApiResponseStatusIs($json, ApiStatus::OK);
|
||||
$this->assertFalse(SingleOptin::query()->where('email', 'singleoptout@hotmail.fr')->exists());
|
||||
$crmCustomer = CustomerInfo::query()->whereHas('customer', function (Builder $query) {
|
||||
$query->where('email', $this->email);
|
||||
})->first();
|
||||
$this->assertTrue(
|
||||
$crmCustomer->opt_in,
|
||||
'Single optin should have been set for customer with email "singleoptin@hotmail.fr"'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -135,7 +139,7 @@ class SingleOptinTest extends ApiBase
|
|||
{
|
||||
$service = Service::query()->firstOrFail();
|
||||
$response = $this->callApiWithToken($service, 'post', route('apiV4.optin'), [
|
||||
'email' => 'singleoptin@hotmail.fr',
|
||||
'email' => $this->email,
|
||||
'opt_in' => 1,
|
||||
'answer_id' => [0],
|
||||
]);
|
||||
|
|
@ -163,7 +167,7 @@ class SingleOptinTest extends ApiBase
|
|||
$singleOptin->refresh();
|
||||
|
||||
$response = $this->callApiWithToken($service, 'post', route('apiV4.optout-hashcode'), [
|
||||
'hashcode' => $singleOptin->hashcode
|
||||
'hashcode' => $singleOptin->hashcode,
|
||||
]);
|
||||
$json = $response->decodeResponseJson();
|
||||
$this->assertApiResponseStatusIs($json, ApiStatus::OK);
|
||||
|
|
@ -194,13 +198,49 @@ class SingleOptinTest extends ApiBase
|
|||
$singleOptin->delete();
|
||||
|
||||
$response = $this->callApiWithToken($service, 'post', route('apiV4.optout-hashcode'), [
|
||||
'hashcode' => $singleOptin->hashcode
|
||||
'hashcode' => $singleOptin->hashcode,
|
||||
]);
|
||||
$json = $response->decodeResponseJson();
|
||||
$this->assertApiResponseStatusIs($json, ApiStatus::KO);
|
||||
$this->assertEquals(ApiError::modelNotFound->value, $json->json('data.code'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test set single optin off using hashcode.
|
||||
* route: apiV4.optout-hashcode
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSetSingleOptoutWithHashcodeWhenCustomerExists(): void
|
||||
{
|
||||
$service = Service::firstOrFail();
|
||||
$this->storeCustomer($service);
|
||||
|
||||
$crmCustomer = CustomerInfo::query()
|
||||
->whereHas('customer', fn(Builder $query) => $query->where('email', $this->email))
|
||||
->firstOrFail();
|
||||
|
||||
$response = $this->callApiWithToken($service, 'post', route('apiV4.optout-hashcode'), [
|
||||
'hashcode' => $crmCustomer->hashcode,
|
||||
]);
|
||||
|
||||
$json = $response->decodeResponseJson();
|
||||
$this->assertApiResponseStatusIs($json, ApiStatus::OK);
|
||||
$this->assertFalse(
|
||||
SingleOptin::query()->where('email', $this->email)->exists(),
|
||||
'A SingleOptin record should not exist for this email.'
|
||||
);
|
||||
|
||||
$updatedCustomer = CustomerInfo::query()
|
||||
->whereHas('customer', fn(Builder $query) => $query->where('email', $this->email))
|
||||
->firstOrFail();
|
||||
$this->assertFalse(
|
||||
$updatedCustomer->opt_in,
|
||||
'Customer "opt_in" field should be set to false after opt-out with hashcode.'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Store a customer in DB.
|
||||
*
|
||||
|
|
@ -214,13 +254,13 @@ class SingleOptinTest extends ApiBase
|
|||
'lastname' => 'Bond',
|
||||
'birth_date' => \date('1992-03-08'),
|
||||
'address' => '10 rue des totos',
|
||||
'email' => 'singleoptin@hotmail.fr',
|
||||
'email' => $this->email,
|
||||
'password' => '123.Mot.De.P4sse',
|
||||
'phone' => '+33654821562',
|
||||
'country' => Country::france->value,
|
||||
'city' => 'Connerré',
|
||||
'postcode' => '72160',
|
||||
'opt_in' => true
|
||||
'opt_in' => true,
|
||||
];
|
||||
$this->callApiWithToken($service, 'post', route('apiV4.customers.store'), $data);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,17 +5,17 @@ namespace Tests\Client;
|
|||
use App\Models\SingleOptin as ModelsSingleOptin;
|
||||
use Illuminate\Support\Str;
|
||||
use LDC\LDCCRM\CrmApiV4;
|
||||
use LDC\LDCCRM\Enums\ApiError;
|
||||
use LDC\LDCCRM\Enums\Country;
|
||||
use LDC\LDCCRM\Exceptions\ApiErrorException;
|
||||
use LDC\LDCCRM\Models\Customer;
|
||||
use LDC\LDCCRM\Models\SingleOptin;
|
||||
|
||||
class CrmClientSingleOptinTest extends CrmClientTester
|
||||
{
|
||||
/** @var string $email */
|
||||
private $email = 'singleoptin@hotmail.fr';
|
||||
|
||||
/**
|
||||
* Create and remove single optin test
|
||||
*
|
||||
* Calling multiple times single optin true should not fail.
|
||||
*
|
||||
* @return void
|
||||
|
|
@ -23,43 +23,43 @@ class CrmClientSingleOptinTest extends CrmClientTester
|
|||
public function testCreateAndRemoveSingleOptin()
|
||||
{
|
||||
// Set single optin and check.
|
||||
$this->assertTrue(SingleOptin::setStatus('singleoptinrandom@gmail.com', true));
|
||||
$this->assertTrue(ModelsSingleOptin::query()->where('email', 'singleoptinrandom@gmail.com')->exists());
|
||||
$this->assertTrue(SingleOptin::setStatus($this->email, true));
|
||||
$this->assertTrue(ModelsSingleOptin::query()->where('email', $this->email)->exists());
|
||||
|
||||
// Set single optin again should not fail.
|
||||
$this->assertTrue(CrmApiV4::setStatus('singleoptinrandom@gmail.com', true));
|
||||
$this->assertTrue(CrmApiV4::setStatus($this->email, true));
|
||||
|
||||
// Remove single optin test and check.
|
||||
$this->assertTrue(SingleOptin::setStatus('singleoptinrandom@gmail.com', false));
|
||||
$this->assertTrue(!ModelsSingleOptin::query()->where('email', 'singleoptinrandom@gmail.com')->exists());
|
||||
$this->assertTrue(SingleOptin::setStatus($this->email, false));
|
||||
$this->assertTrue(!ModelsSingleOptin::query()->where('email', $this->email)->exists());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test create single optin should fail if a customer exists.
|
||||
* Test set single optin on when customer exists.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCreateSingleOptinShouldFailBecauseCustomerExists()
|
||||
public function testSetSingleOptinWhenCustomerExists()
|
||||
{
|
||||
$this->expectException(ApiErrorException::class);
|
||||
$this->expectExceptionMessage(ApiError::singleOptinCustomerExists->label());
|
||||
$customer = new Customer();
|
||||
|
||||
$randomMail = Str::uuid() . 'totosingleoptin.delarogerie@gmail.com';
|
||||
|
||||
$customer->firstname = 'toto singleoptin';
|
||||
$customer->lastname = 'delarogerie';
|
||||
$customer->email = $randomMail;
|
||||
$customer->password = 'Complic4tEd!P4assW0rd';
|
||||
$customer->birth_date = '1992-03-08';
|
||||
$customer->phone = '0673891160';
|
||||
$customer->address = 'rue des totos';
|
||||
$customer->city = 'Toto ville';
|
||||
$customer->postcode = '72160';
|
||||
$customer->country = Country::france->value();
|
||||
$customer = new Customer();
|
||||
$customer->fill([
|
||||
'firstname' => 'toto singleoptin',
|
||||
'lastname' => 'delarogerie',
|
||||
'email' => $randomMail,
|
||||
'password' => 'Complic4tEd!P4assW0rd',
|
||||
'birth_date' => '1992-03-08',
|
||||
'phone' => '0673891160',
|
||||
'address' => 'rue des totos',
|
||||
'city' => 'Toto ville',
|
||||
'postcode' => '72160',
|
||||
'country' => Country::france->value(),
|
||||
'opt_in' => false,
|
||||
]);
|
||||
|
||||
$this->assertTrue($customer->save());
|
||||
$this->assertFalse(SingleOptin::setStatus($randomMail, true));
|
||||
$this->assertTrue(SingleOptin::setStatus($randomMail, true));
|
||||
$this->assertTrue(Customer::findWithEmail($randomMail)->opt_in);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -72,14 +72,44 @@ class CrmClientSingleOptinTest extends CrmClientTester
|
|||
public function testRemoveSingleOptinWithHashcode()
|
||||
{
|
||||
// Set single optin and check.
|
||||
$this->assertTrue(SingleOptin::setStatus('singleoptinrandom@gmail.com', true));
|
||||
$this->assertTrue(ModelsSingleOptin::query()->where('email', 'singleoptinrandom@gmail.com')->exists());
|
||||
$this->assertTrue(SingleOptin::setStatus($this->email, true));
|
||||
$this->assertTrue(ModelsSingleOptin::query()->where('email', $this->email)->exists());
|
||||
|
||||
$hashcode = ModelsSingleOptin::query()
|
||||
->where('email', 'singleoptinrandom@gmail.com')->sole('hashcode')->hashcode;
|
||||
->where('email', $this->email)->sole('hashcode')->hashcode;
|
||||
|
||||
// Remove single optin test and check.
|
||||
$this->assertTrue(SingleOptin::unsetStatusWithHashcode($hashcode));
|
||||
$this->assertTrue(!ModelsSingleOptin::query()->where('email', 'singleoptinrandom@gmail.com')->exists());
|
||||
$this->assertTrue(!ModelsSingleOptin::query()->where('email', $this->email)->exists());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove single optin with hashcode test
|
||||
*
|
||||
* Calling multiple times single optin true should not fail.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRemoveSingleOptinWithHashcodeWhenCustomerExists()
|
||||
{
|
||||
$randomMail = Str::uuid() . 'totosingleoptin.delarogerie@gmail.com';
|
||||
$customer = new Customer();
|
||||
$customer->fill([
|
||||
'firstname' => 'toto singleoptin',
|
||||
'lastname' => 'delarogerie',
|
||||
'email' => $randomMail,
|
||||
'password' => 'Complic4tEd!P4assW0rd',
|
||||
'birth_date' => '1992-03-08',
|
||||
'phone' => '0673891160',
|
||||
'address' => 'rue des totos',
|
||||
'city' => 'Toto ville',
|
||||
'postcode' => '72160',
|
||||
'country' => Country::france->value(),
|
||||
'opt_in' => true,
|
||||
]);
|
||||
|
||||
$this->assertTrue($customer->save());
|
||||
$this->assertTrue(SingleOptin::setStatus($randomMail, false));
|
||||
$this->assertFalse(Customer::findWithEmail($randomMail)->opt_in);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue