-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: include honorific with new donors during donation form request (#…
…7101) Co-authored-by: Jon Waldstein <[email protected]>
- Loading branch information
1 parent
21f163d
commit 51e813a
Showing
4 changed files
with
182 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Give\DonationForms\Actions; | ||
|
||
use Exception; | ||
use Give\Donors\Models\Donor; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
class GetOrCreateDonor | ||
{ | ||
public $donorCreated = false; | ||
|
||
/** | ||
* @unreleased | ||
* | ||
* @throws Exception | ||
*/ | ||
public function __invoke( | ||
?int $userId, | ||
string $donorEmail, | ||
string $firstName, | ||
string $lastName, | ||
?string $honorific | ||
): Donor { | ||
// first check if donor exists as a user | ||
$donor = $userId ? Donor::whereUserId($userId) : null; | ||
|
||
// If they exist as a donor & user then make sure they don't already own this email before adding to their additional emails list.. | ||
if ($donor && !$donor->hasEmail($donorEmail) && !Donor::whereEmail($donorEmail)) { | ||
$donor->additionalEmails = array_merge($donor->additionalEmails ?? [], [$donorEmail]); | ||
$donor->save(); | ||
} | ||
|
||
// if donor is not a user than check for any donor matching this email | ||
if (!$donor) { | ||
$donor = Donor::whereEmail($donorEmail); | ||
} | ||
|
||
// if no donor exists then create a new one using their personal information from the form. | ||
if (!$donor) { | ||
$donor = Donor::create([ | ||
'name' => trim("$firstName $lastName"), | ||
'firstName' => $firstName, | ||
'lastName' => $lastName, | ||
'email' => $donorEmail, | ||
'userId' => $userId ?: null, | ||
'prefix' => $honorific, | ||
]); | ||
|
||
$this->donorCreated = true; | ||
} | ||
|
||
return $donor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Give\Tests\Unit\DonationForms\Actions; | ||
|
||
use Exception; | ||
use Give\DonationForms\Actions\GetOrCreateDonor; | ||
use Give\Donors\Models\Donor; | ||
use Give\Tests\TestCase; | ||
use Give\Tests\TestTraits\RefreshDatabase; | ||
|
||
class TestGetOrCreateDonor extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
/** | ||
* @unreleased | ||
* | ||
* @throws Exception | ||
*/ | ||
public function testShouldReturnExistingDonorWithMatchingEmail(): void | ||
{ | ||
$donor = Donor::factory()->create(['userId' => 1]); | ||
$action = new GetOrCreateDonor(); | ||
$donorFromActionWithMatchingEmail = $action(null, $donor->email, $donor->firstName, $donor->lastName, $donor->prefix); | ||
|
||
$this->assertEquals($donor->toArray(), $donorFromActionWithMatchingEmail->toArray()); | ||
$this->assertFalse($action->donorCreated); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
* | ||
* @throws Exception | ||
*/ | ||
public function testShouldReturnExistingDonorWithMatchingUserId(): void | ||
{ | ||
$donor = Donor::factory()->create(['userId' => 1]); | ||
$action = new GetOrCreateDonor(); | ||
$donorFromActionWithMatchingUserId = $action($donor->userId, $donor->email, 'billing first name', 'billing last name', null); | ||
|
||
$this->assertEquals($donor->toArray(), $donorFromActionWithMatchingUserId->toArray()); | ||
$this->assertFalse($action->donorCreated); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
* @throws Exception | ||
*/ | ||
public function testShouldReturnExistingDonorWithUserIdAndUpdateAdditionalEmails(): void | ||
{ | ||
$donor = Donor::factory()->create(['userId' => 1]); | ||
$action = new GetOrCreateDonor(); | ||
$donorFromActionWithMatchingUserId = $action($donor->userId, '[email protected]', 'billing first name', 'billing last name', null); | ||
$donor->additionalEmails = array_merge($donor->additionalEmails ?? [], ['[email protected]']); | ||
$donor->save(); | ||
|
||
$this->assertEquals($donor->toArray(), $donorFromActionWithMatchingUserId->toArray()); | ||
$this->assertFalse($action->donorCreated); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
* | ||
* @throws Exception | ||
*/ | ||
public function testShouldReturnExistingDonorWithUserIdAndNotUpdateAdditionalEmails(): void | ||
{ | ||
$donor = Donor::factory()->create(['userId' => 1]); | ||
$donorWithExistingEmail = Donor::factory()->create(); | ||
$action = new GetOrCreateDonor(); | ||
$donorFromActionWithMatchingUserId = $action($donor->userId, $donorWithExistingEmail->email, 'billing first name', 'billing last name', null); | ||
|
||
$this->assertEquals($donor->toArray(), $donorFromActionWithMatchingUserId->toArray()); | ||
$this->assertFalse($action->donorCreated); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
* | ||
* @throws Exception | ||
*/ | ||
public function testShouldReturnNewDonor(): void | ||
{ | ||
$action = new GetOrCreateDonor(); | ||
$donorFromAction = $action(null, '[email protected]', 'Bill', 'Murray', 'Mr.'); | ||
|
||
$this->assertSame('Bill Murray', $donorFromAction->name); | ||
$this->assertSame('Bill', $donorFromAction->firstName); | ||
$this->assertSame('Murray', $donorFromAction->lastName); | ||
$this->assertSame('Mr.', $donorFromAction->prefix); | ||
$this->assertSame('[email protected]', $donorFromAction->email); | ||
$this->assertTrue($action->donorCreated); | ||
} | ||
} |