From 8dae92ac841c2ed57f72a56d5e1a368774220f73 Mon Sep 17 00:00:00 2001 From: Abban Dunne Date: Fri, 15 Sep 2023 13:29:18 +0200 Subject: [PATCH] Add CompanyContactName Test C23_WMDE_Desktop_DE_05 makes company donors input name fields. This is a new DonorName implementation to handle that. Ticket: https://phabricator.wikimedia.org/T345528 --- src/DataAccess/DonorFactory.php | 10 ++- src/Domain/Model/Donor/CompanyDonor.php | 3 +- .../Model/Donor/Name/CompanyContactName.php | 49 ++++++++++++++ src/Domain/Model/Donor/Name/CompanyName.php | 3 + .../AddDonation/AddDonationUseCase.php | 10 ++- .../UpdateDonor/UpdateDonorUseCase.php | 10 ++- tests/Data/ValidDoctrineDonation.php | 3 +- tests/Data/ValidDonation.php | 12 +++- .../AddDonation/AddDonationUseCaseTest.php | 4 +- .../Domain/Model/CompanyContactNameTest.php | 64 +++++++++++++++++++ 10 files changed, 155 insertions(+), 13 deletions(-) create mode 100644 src/Domain/Model/Donor/Name/CompanyContactName.php create mode 100644 tests/Unit/Domain/Model/CompanyContactNameTest.php diff --git a/src/DataAccess/DonorFactory.php b/src/DataAccess/DonorFactory.php index 2ce7186f..e39f154e 100644 --- a/src/DataAccess/DonorFactory.php +++ b/src/DataAccess/DonorFactory.php @@ -9,7 +9,7 @@ use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Address\PostalAddress; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\AnonymousDonor; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\CompanyDonor; -use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyName; +use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyContactName; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\PersonName; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\PersonDonor; @@ -31,7 +31,13 @@ public static function createDonorFromEntity( DoctrineDonation $donation ): Dono ); case 'firma': return new CompanyDonor( - new CompanyName( $data->getValue( 'firma' ) ), + new CompanyContactName( + $data->getValue( 'firma' ), + $data->getValue( 'vorname' ), + $data->getValue( 'nachname' ), + $data->getValue( 'anrede' ), + $data->getValue( 'titel' ) + ), self::createPhysicalAddress( $data ), $donation->getDonorEmail() ?? '' ); diff --git a/src/Domain/Model/Donor/CompanyDonor.php b/src/Domain/Model/Donor/CompanyDonor.php index 1510294e..f5be8e13 100644 --- a/src/Domain/Model/Donor/CompanyDonor.php +++ b/src/Domain/Model/Donor/CompanyDonor.php @@ -5,6 +5,7 @@ namespace WMDE\Fundraising\DonationContext\Domain\Model\Donor; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Address\PostalAddress; +use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyContactName; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyName; use WMDE\Fundraising\DonationContext\Domain\Model\DonorType; @@ -12,7 +13,7 @@ class CompanyDonor extends AbstractDonor { use NewsletterTrait; use ReceiptTrait; - public function __construct( CompanyName $name, PostalAddress $address, string $emailAddress ) { + public function __construct( CompanyName|CompanyContactName $name, PostalAddress $address, string $emailAddress ) { $this->name = $name; $this->physicalAddress = $address; $this->emailAddress = $emailAddress; diff --git a/src/Domain/Model/Donor/Name/CompanyContactName.php b/src/Domain/Model/Donor/Name/CompanyContactName.php new file mode 100644 index 00000000..73beec2c --- /dev/null +++ b/src/Domain/Model/Donor/Name/CompanyContactName.php @@ -0,0 +1,49 @@ +companyName; + + if ( $this->title !== '' || $this->firstName !== '' || $this->lastName !== '' ) { + $name .= ' - ' . implode( + ' ', + array_filter( [ + $this->title, + $this->firstName, + $this->lastName + ] ) + ); + } + + return $name; + } + + public function toArray(): array { + return array_merge( [ + 'companyName' => $this->companyName, + ], + array_filter( [ + 'salutation' => $this->salutation, + 'title' => $this->title, + 'firstName' => $this->firstName, + 'lastName' => $this->lastName + ] ) + ); + } +} diff --git a/src/Domain/Model/Donor/Name/CompanyName.php b/src/Domain/Model/Donor/Name/CompanyName.php index a16a0d4e..6fc6b6b2 100644 --- a/src/Domain/Model/Donor/Name/CompanyName.php +++ b/src/Domain/Model/Donor/Name/CompanyName.php @@ -6,6 +6,9 @@ use WMDE\Fundraising\DonationContext\Domain\Model\DonorName; +/** + * @deprecated Please use CompanyContactName instead + */ class CompanyName implements DonorName { private string $companyName; diff --git a/src/UseCases/AddDonation/AddDonationUseCase.php b/src/UseCases/AddDonation/AddDonationUseCase.php index d356b469..10f3824f 100644 --- a/src/UseCases/AddDonation/AddDonationUseCase.php +++ b/src/UseCases/AddDonation/AddDonationUseCase.php @@ -13,7 +13,7 @@ use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Address\PostalAddress; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\AnonymousDonor; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\CompanyDonor; -use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyName; +use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyContactName; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\PersonName; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\PersonDonor; use WMDE\Fundraising\DonationContext\Domain\Model\DonorType; @@ -117,7 +117,13 @@ private function getPersonalInfoFromRequest( AddDonationRequest $request ): Dono ); } elseif ( $donorType->is( DonorType::COMPANY() ) ) { return new CompanyDonor( - new CompanyName( $request->getDonorCompany() ), + new CompanyContactName( + $request->getDonorCompany(), + $request->getDonorFirstName(), + $request->getDonorLastName(), + $request->getDonorSalutation(), + $request->getDonorTitle() + ), $this->getPhysicalAddressFromRequest( $request ), $request->getDonorEmailAddress() ); diff --git a/src/UseCases/UpdateDonor/UpdateDonorUseCase.php b/src/UseCases/UpdateDonor/UpdateDonorUseCase.php index 2a970bed..b900ed87 100644 --- a/src/UseCases/UpdateDonor/UpdateDonorUseCase.php +++ b/src/UseCases/UpdateDonor/UpdateDonorUseCase.php @@ -9,7 +9,7 @@ use WMDE\Fundraising\DonationContext\Domain\Model\Donor; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Address\PostalAddress; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\CompanyDonor; -use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyName; +use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyContactName; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\PersonName; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\PersonDonor; use WMDE\Fundraising\DonationContext\Domain\Model\DonorType; @@ -108,7 +108,13 @@ private function getDonorFromRequest( UpdateDonorRequest $updateDonorRequest ): } elseif ( $updateDonorRequest->getDonorType()->is( DonorType::COMPANY() ) ) { return new CompanyDonor( - new CompanyName( $updateDonorRequest->getCompanyName() ), + new CompanyContactName( + $updateDonorRequest->getCompanyName(), + $updateDonorRequest->getFirstName(), + $updateDonorRequest->getLastName(), + $updateDonorRequest->getSalutation(), + $updateDonorRequest->getTitle() + ), $this->getDonorAddressFromRequest( $updateDonorRequest ), $updateDonorRequest->getEmailAddress() ); diff --git a/tests/Data/ValidDoctrineDonation.php b/tests/Data/ValidDoctrineDonation.php index b76a98d4..b6c5607d 100644 --- a/tests/Data/ValidDoctrineDonation.php +++ b/tests/Data/ValidDoctrineDonation.php @@ -58,7 +58,8 @@ public static function newCompanyDonation(): Donation { 'firma' => ValidDonation::DONOR_COMPANY ], $self->getAddressArray(), - [ 'email' => ValidDonation::DONOR_EMAIL_ADDRESS ] + [ 'email' => ValidDonation::DONOR_EMAIL_ADDRESS ], + $self->getPersonNameArray() ) ); return $donation; diff --git a/tests/Data/ValidDonation.php b/tests/Data/ValidDonation.php index e0cce914..7dbd6bbe 100644 --- a/tests/Data/ValidDonation.php +++ b/tests/Data/ValidDonation.php @@ -11,7 +11,7 @@ use WMDE\Fundraising\DonationContext\Domain\Model\Donor\AnonymousDonor; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\CompanyDonor; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\EmailDonor; -use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyName; +use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyContactName; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\PersonName; use WMDE\Fundraising\DonationContext\Domain\Model\Donor\PersonDonor; use WMDE\Fundraising\PaymentContext\Domain\Model\Payment; @@ -245,8 +245,14 @@ public static function newCompanyDonor(): CompanyDonor { ); } - private static function newCompanyName(): CompanyName { - return new CompanyName( self::DONOR_COMPANY ); + private static function newCompanyName(): CompanyContactName { + return new CompanyContactName( + self::DONOR_COMPANY, + self::DONOR_FIRST_NAME, + self::DONOR_LAST_NAME, + self::DONOR_SALUTATION, + self::DONOR_TITLE + ); } public static function newEmailOnlyDonor(): EmailDonor { diff --git a/tests/Integration/UseCases/AddDonation/AddDonationUseCaseTest.php b/tests/Integration/UseCases/AddDonation/AddDonationUseCaseTest.php index 0091eece..016624f2 100644 --- a/tests/Integration/UseCases/AddDonation/AddDonationUseCaseTest.php +++ b/tests/Integration/UseCases/AddDonation/AddDonationUseCaseTest.php @@ -9,7 +9,7 @@ use WMDE\Fundraising\DonationContext\Authorization\DonationTokens; use WMDE\Fundraising\DonationContext\Domain\Event\DonationCreatedEvent; use WMDE\Fundraising\DonationContext\Domain\Model\Donation; -use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyName; +use WMDE\Fundraising\DonationContext\Domain\Model\Donor\Name\CompanyContactName; use WMDE\Fundraising\DonationContext\Domain\Model\DonorType; use WMDE\Fundraising\DonationContext\Domain\Model\ModerationIdentifier; use WMDE\Fundraising\DonationContext\Domain\Model\ModerationReason; @@ -295,7 +295,7 @@ public function testEventIsEmittedAfterDonationWasStored(): void { $events = $eventEmitter->getEvents(); $this->assertCount( 1, $events, 'Only 1 event should be emitted' ); $this->assertInstanceOf( DonationCreatedEvent::class, $events[0] ); - $this->assertInstanceOf( CompanyName::class, $events[0]->getDonor()->getName() ); + $this->assertInstanceOf( CompanyContactName::class, $events[0]->getDonor()->getName() ); } public function testWhenEmailAddressIsBlacklisted_donationIsMarkedAsCancelled(): void { diff --git a/tests/Unit/Domain/Model/CompanyContactNameTest.php b/tests/Unit/Domain/Model/CompanyContactNameTest.php new file mode 100644 index 00000000..0875345a --- /dev/null +++ b/tests/Unit/Domain/Model/CompanyContactNameTest.php @@ -0,0 +1,64 @@ +assertSame( 'Scrooge and Marley', $companyName->getFullName() ); + } + + public function testGivenNameWithoutSalutation_getFullNameReturnsNameWithoutSalutation(): void { + $companyName = new CompanyContactName( 'Scrooge and Marley', 'Ebenezer', 'Scrooge', '', '' ); + + $this->assertSame( 'Scrooge and Marley - Ebenezer Scrooge', $companyName->getFullName() ); + } + + public function testGivenNameWithSalutation_getFullNameReturnsNameWithoutSalutation(): void { + $companyName = new CompanyContactName( 'Scrooge and Marley', 'Ebenezer', 'Scrooge', 'Sir', '' ); + + $this->assertSame( 'Scrooge and Marley - Ebenezer Scrooge', $companyName->getFullName() ); + } + + public function testGivenNameWithTitle_getFullNameReturnsNameWithTitle(): void { + $companyName = new CompanyContactName( 'Scrooge and Marley', 'Ebenezer', 'Scrooge', '', 'Prof. Dr.' ); + + $this->assertSame( 'Scrooge and Marley - Prof. Dr. Ebenezer Scrooge', $companyName->getFullName() ); + } + + public function testGivenNameWithTitleAndSalutation_getFullNameReturnsNameWithTitle(): void { + $companyName = new CompanyContactName( 'Scrooge and Marley', 'Ebenezer', 'Scrooge', 'Herr', 'Prof. Dr.' ); + + $this->assertSame( 'Scrooge and Marley - Prof. Dr. Ebenezer Scrooge', $companyName->getFullName() ); + } + + public function testGivenCompanyNameOnly_toArrayReturnsCompanyName(): void { + $companyName = new CompanyContactName( 'Scrooge and Marley', '', '', '', '' ); + + $this->assertEquals( [ 'companyName' => 'Scrooge and Marley', ], $companyName->toArray() ); + } + + public function testToArrayReturnsAllFields(): void { + $companyName = new CompanyContactName( 'Scrooge and Marley', 'Ebenezer', 'Scrooge', 'Herr', 'Prof. Dr.' ); + + $this->assertEquals( + [ + 'companyName' => 'Scrooge and Marley', + 'salutation' => 'Herr', + 'title' => 'Prof. Dr.', + 'firstName' => 'Ebenezer', + 'lastName' => 'Scrooge' + ], + $companyName->toArray() + ); + } + +}