|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for BaseCarrier::parseRecipients() |
| 5 | + * |
| 6 | + * @package notification |
| 7 | + */ |
| 8 | + |
| 9 | +namespace BracketSpace\Notification\Tests\Carriers; |
| 10 | + |
| 11 | +use BracketSpace\Notification\Interfaces\Triggerable; |
| 12 | +use BracketSpace\Notification\Repository\Carrier\Email; |
| 13 | +use BracketSpace\Notification\Store\Recipient as RecipientStore; |
| 14 | +use BracketSpace\Notification\Tests\Helpers\Objects\CarrierWithRecipients; |
| 15 | +use BracketSpace\Notification\Tests\Helpers\Objects\RecipientWithReturnField; |
| 16 | + |
| 17 | +it('clones recipients before parsing to prevent shared state mutation', function () { |
| 18 | + $carrier = new CarrierWithRecipients('clone_test'); |
| 19 | + $recipient = new RecipientWithReturnField('dummy_return_field', 'user_email'); |
| 20 | + |
| 21 | + RecipientStore::insert('clone_test', 'dummy_return_field', $recipient); |
| 22 | + |
| 23 | + $carrier->setRecipientsResolvedData([ |
| 24 | + ['type' => 'dummy_return_field', 'recipient' => 'test_value'], |
| 25 | + ]); |
| 26 | + |
| 27 | + // Add filter that modifies the recipient it receives. |
| 28 | + add_filter('notification/recipient/pre_parse_value', function ($recipientObj) { |
| 29 | + if (method_exists($recipientObj, 'setReturnField')) { |
| 30 | + $recipientObj->setReturnField('ID'); |
| 31 | + } |
| 32 | + return $recipientObj; |
| 33 | + }); |
| 34 | + |
| 35 | + $carrier->parseRecipients(); |
| 36 | + |
| 37 | + // The original store instance must remain unchanged. |
| 38 | + $storeRecipient = RecipientStore::get('clone_test', 'dummy_return_field'); |
| 39 | + expect($storeRecipient->getReturnField())->toBe('user_email'); |
| 40 | + |
| 41 | + remove_all_filters('notification/recipient/pre_parse_value'); |
| 42 | +}); |
| 43 | + |
| 44 | +it('sets return field on recipients supporting HasReturnField when carrier declares one', function () { |
| 45 | + $carrier = new class ('return_field_test') extends CarrierWithRecipients { |
| 46 | + protected function getRecipientReturnField(): ?string |
| 47 | + { |
| 48 | + return 'ID'; |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + $recipient = new RecipientWithReturnField('dummy_return_field', 'user_email'); |
| 53 | + RecipientStore::insert('return_field_test', 'dummy_return_field', $recipient); |
| 54 | + |
| 55 | + $carrier->setRecipientsResolvedData([ |
| 56 | + ['type' => 'dummy_return_field', 'recipient' => 'val'], |
| 57 | + ]); |
| 58 | + |
| 59 | + $result = $carrier->parseRecipients(); |
| 60 | + |
| 61 | + // The carrier declares 'ID', so parsed output should use 'ID', not 'user_email'. |
| 62 | + expect($result)->toBe(['ID:val']); |
| 63 | +}); |
| 64 | + |
| 65 | +it('does not override return field when carrier returns null', function () { |
| 66 | + $carrier = new CarrierWithRecipients('null_field_test'); |
| 67 | + |
| 68 | + $recipient = new RecipientWithReturnField('dummy_return_field', 'ID'); |
| 69 | + RecipientStore::insert('null_field_test', 'dummy_return_field', $recipient); |
| 70 | + |
| 71 | + $carrier->setRecipientsResolvedData([ |
| 72 | + ['type' => 'dummy_return_field', 'recipient' => 'val'], |
| 73 | + ]); |
| 74 | + |
| 75 | + $result = $carrier->parseRecipients(); |
| 76 | + |
| 77 | + // Carrier returns null for getRecipientReturnField, so recipient's own 'ID' should be used. |
| 78 | + expect($result)->toBe(['ID:val']); |
| 79 | +}); |
| 80 | + |
| 81 | +it('fires notification/recipient/pre_parse_value filter', function () { |
| 82 | + $carrier = new CarrierWithRecipients('filter_test'); |
| 83 | + $recipient = new RecipientWithReturnField('dummy_return_field', 'user_email'); |
| 84 | + |
| 85 | + RecipientStore::insert('filter_test', 'dummy_return_field', $recipient); |
| 86 | + |
| 87 | + $carrier->setRecipientsResolvedData([ |
| 88 | + ['type' => 'dummy_return_field', 'recipient' => 'test_value'], |
| 89 | + ]); |
| 90 | + |
| 91 | + $filterCalled = false; |
| 92 | + $capturedArgs = []; |
| 93 | + |
| 94 | + add_filter('notification/recipient/pre_parse_value', function ($recipientObj, $carrierSlug, $type) use (&$filterCalled, &$capturedArgs) { |
| 95 | + $filterCalled = true; |
| 96 | + $capturedArgs = [ |
| 97 | + 'carrier_slug' => $carrierSlug, |
| 98 | + 'type' => $type, |
| 99 | + ]; |
| 100 | + return $recipientObj; |
| 101 | + }, 10, 3); |
| 102 | + |
| 103 | + $carrier->parseRecipients(); |
| 104 | + |
| 105 | + expect($filterCalled)->toBeTrue(); |
| 106 | + expect($capturedArgs['carrier_slug'])->toBe('filter_test'); |
| 107 | + expect($capturedArgs['type'])->toBe('dummy_return_field'); |
| 108 | + |
| 109 | + remove_all_filters('notification/recipient/pre_parse_value'); |
| 110 | +}); |
| 111 | + |
| 112 | +it('returns user_email from getRecipientReturnField for Email carrier', function () { |
| 113 | + $email = new Email(); |
| 114 | + |
| 115 | + $reflection = new \ReflectionMethod($email, 'getRecipientReturnField'); |
| 116 | + $reflection->setAccessible(true); |
| 117 | + |
| 118 | + expect($reflection->invoke($email))->toBe('user_email'); |
| 119 | +}); |
| 120 | + |
| 121 | +it('returns null from getRecipientReturnField by default', function () { |
| 122 | + $carrier = new CarrierWithRecipients(); |
| 123 | + |
| 124 | + $reflection = new \ReflectionMethod($carrier, 'getRecipientReturnField'); |
| 125 | + $reflection->setAccessible(true); |
| 126 | + |
| 127 | + expect($reflection->invoke($carrier))->toBeNull(); |
| 128 | +}); |
0 commit comments