Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: update meta persistence in donor and donation repositories #7679

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/Donations/Repositories/DonationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public function queryByDonorId(int $donorId): ModelQueryBuilder
}

/**
* @unreleased store meta using native WP functions
* @since 2.23.0 retrieve the post_parent instead of relying on parentId property
* @since 2.21.0 replace actions with givewp_donation_creating and givewp_donation_created
* @since 2.20.0 mutate model and return void
Expand Down Expand Up @@ -204,12 +205,7 @@ public function insert(Donation $donation)
$donationMeta = $this->getCoreDonationMetaForDatabase($donation);

foreach ($donationMeta as $metaKey => $metaValue) {
DB::table('give_donationmeta')
->insert([
'donation_id' => $donationId,
'meta_key' => $metaKey,
'meta_value' => $metaValue,
]);
give()->payment_meta->add_meta($donationId, $metaKey, $metaValue);
}
} catch (Exception $exception) {
DB::query('ROLLBACK');
Expand Down
24 changes: 5 additions & 19 deletions src/Donors/Repositories/DonorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function getAdditionalEmails(int $donorId)
}

/**
* @unreleased store meta using native WP functions
* @since 3.7.0 Add support to "phone" property
* @since 2.24.0 add support for $donor->totalAmountDonated and $donor->totalNumberOfDonation
* @since 2.21.0 add actions givewp_donor_creating and givewp_donor_created
Expand Down Expand Up @@ -142,22 +143,12 @@ public function insert(Donor $donor)
$donorId = DB::last_insert_id();

foreach ($this->getCoreDonorMeta($donor) as $metaKey => $metaValue) {
DB::table('give_donormeta')
->insert([
'donor_id' => $donorId,
'meta_key' => $metaKey,
'meta_value' => $metaValue,
]);
give()->donor_meta->add_meta($donorId, $metaKey, $metaValue);
}

if (isset($donor->additionalEmails)) {
foreach ($donor->additionalEmails as $additionalEmail) {
DB::table('give_donormeta')
->insert([
'donor_id' => $donorId,
'meta_key' => DonorMetaKeys::ADDITIONAL_EMAILS,
'meta_value' => $additionalEmail,
]);
give()->donor_meta->add_meta($donorId, DonorMetaKeys::ADDITIONAL_EMAILS, $additionalEmail);
}
}
} catch (Exception $exception) {
Expand Down Expand Up @@ -411,6 +402,7 @@ public function prepareQuery(): DonorModelQueryBuilder
* Additional emails are assigned to the same additional_email meta key.
* In order to update them we need to delete and re-insert.
*
* @unreleased store meta using native WP functions
* @since 2.19.6
*
* @return void
Expand All @@ -426,13 +418,7 @@ private function updateAdditionalEmails(Donor $donor)
}

foreach ($donor->additionalEmails as $additionalEmail) {
DB::table('give_donormeta')
->where('donor_id', $donor->id)
->insert([
'donor_id' => $donor->id,
'meta_key' => DonorMetaKeys::ADDITIONAL_EMAILS,
'meta_value' => $additionalEmail,
]);
give()->donor_meta->add_meta($donor->id, DonorMetaKeys::ADDITIONAL_EMAILS, $additionalEmail);
}
}

Expand Down
29 changes: 29 additions & 0 deletions tests/Unit/Donations/Repositories/TestDonationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Exception;
use Give\Donations\Models\Donation;
use Give\Donations\Repositories\DonationRepository;
use Give\Donations\ValueObjects\DonationMetaKeys;
use Give\Donations\ValueObjects\DonationStatus;
use Give\Donations\ValueObjects\DonationType;
use Give\Donors\Models\Donor;
Expand Down Expand Up @@ -267,4 +268,32 @@ public function testUpdateDonationShouldClearPostCache()

$this->assertEquals($donationStatus, $postStatus);
}

/**
* @unreleased
* @throws \Give\Framework\Exceptions\Primitives\Exception
*/
public function testInsertShouldSafelyStoreMetaValues(): void
{
$name = (object)['first' => 'Jon', 'last' => 'Doe'];

$serializedFirstName = serialize($name);

$donation = new Donation(array_merge(Donation::factory()->definition(), [
'firstName' => $serializedFirstName,
]));

$repository = new DonationRepository();

$repository->insert($donation);

$metaValue = give()->payment_meta->get_meta($donation->id, DonationMetaKeys::FIRST_NAME, true);
$metaQuery = DB::table('give_donationmeta')
->where('donation_id', $donation->id)
->where('meta_key', DonationMetaKeys::FIRST_NAME)
->get();

$this->assertSame($serializedFirstName, $metaValue);
$this->assertSame(serialize($serializedFirstName), $metaQuery->meta_value);
}
}
29 changes: 29 additions & 0 deletions tests/Unit/Donors/Repositories/TestDonorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Give\Donors\Models\Donor;
use Give\Donors\Repositories\DonorRepository;
use Give\Donors\ValueObjects\DonorMetaKeys;
use Give\Framework\Database\DB;
use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
use Give\Subscriptions\Repositories\SubscriptionRepository;
Expand Down Expand Up @@ -211,4 +212,32 @@ public function testDeleteShouldRemoveDonorFromTheDatabase()
$this->assertNull($donorQuery);
$this->assertEmpty($donorMetaQuery);
}

/**
* @unreleased
* @throws \Give\Framework\Exceptions\Primitives\Exception|Exception
*/
public function testInsertShouldSafelyStoreMetaValues(): void
{
$name = (object)['first' => 'Jon', 'last' => 'Doe'];

$serializedFirstName = serialize($name);

$donor = new Donor(array_merge(Donor::factory()->definition(), [
'firstName' => $serializedFirstName,
]));

$repository = new DonorRepository();

$repository->insert($donor);

$metaValue = give()->donor_meta->get_meta($donor->id, DonorMetaKeys::FIRST_NAME, true);
$metaQuery = DB::table('give_donormeta')
->where('donor_id', $donor->id)
->where('meta_key', DonorMetaKeys::FIRST_NAME)
->get();

$this->assertSame($serializedFirstName, $metaValue);
$this->assertSame(serialize($serializedFirstName), $metaQuery->meta_value);
}
}
Loading