|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Setono\MetaConversionsApi\Cookie; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | + |
| 9 | +final class CookieResolverTest extends TestCase |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @test |
| 13 | + */ |
| 14 | + public function it_passes_through_existing_five_segment_cookies_unchanged(): void |
| 15 | + { |
| 16 | + $fbc = 'fb.1.1657051589577.IwAR1a-b_c.AQECAQMB'; |
| 17 | + $fbp = 'fb.1.1656874832584.1088522659.AQEAAQMB'; |
| 18 | + |
| 19 | + $resolvedCookies = (new CookieResolver())->resolve('www.example.com', [], ['_fbc' => $fbc, '_fbp' => $fbp]); |
| 20 | + |
| 21 | + self::assertNotNull($resolvedCookies->fbc); |
| 22 | + self::assertSame($fbc, $resolvedCookies->fbc->value()); |
| 23 | + self::assertNotNull($resolvedCookies->fbp); |
| 24 | + self::assertSame($fbp, $resolvedCookies->fbp->value()); |
| 25 | + self::assertSame([], $resolvedCookies->cookiesToSet); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @test |
| 30 | + */ |
| 31 | + public function it_upgrades_a_four_segment_cookie_with_an_appendix_and_sets_it(): void |
| 32 | + { |
| 33 | + $fbp = 'fb.1.1656874832584.1088522659'; |
| 34 | + |
| 35 | + $resolvedCookies = (new CookieResolver())->resolve('www.example.com', [], ['_fbp' => $fbp]); |
| 36 | + |
| 37 | + self::assertNotNull($resolvedCookies->fbp); |
| 38 | + self::assertMatchesRegularExpression('/^fb\.1\.1656874832584\.1088522659\.[A-Za-z0-9_-]{8}$/', $resolvedCookies->fbp->value()); |
| 39 | + self::assertNotNull($resolvedCookies->fbp->getAppendix()); |
| 40 | + |
| 41 | + $cookie = self::cookie($resolvedCookies, '_fbp'); |
| 42 | + self::assertSame($resolvedCookies->fbp->value(), $cookie->value); |
| 43 | + self::assertSame(90 * 24 * 3600, $cookie->maxAge); |
| 44 | + self::assertSame('example.com', $cookie->domain); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @test |
| 49 | + */ |
| 50 | + public function it_builds_an_fbc_from_the_fbclid_query_parameter(): void |
| 51 | + { |
| 52 | + $before = (int) floor(microtime(true) * 1000); |
| 53 | + $resolvedCookies = (new CookieResolver())->resolve('www.example.com', ['fbclid' => 'IwAR1a-b_c'], []); |
| 54 | + $after = (int) ceil(microtime(true) * 1000); |
| 55 | + |
| 56 | + self::assertNotNull($resolvedCookies->fbc); |
| 57 | + self::assertSame('IwAR1a-b_c', $resolvedCookies->fbc->getClickId()); |
| 58 | + self::assertSame(1, $resolvedCookies->fbc->getSubdomainIndex()); |
| 59 | + self::assertGreaterThanOrEqual($before, $resolvedCookies->fbc->getCreationTime()); |
| 60 | + self::assertLessThanOrEqual($after, $resolvedCookies->fbc->getCreationTime()); |
| 61 | + self::assertNotNull($resolvedCookies->fbc->getAppendix()); |
| 62 | + |
| 63 | + self::assertSame($resolvedCookies->fbc->value(), self::cookie($resolvedCookies, '_fbc')->value); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @test |
| 68 | + */ |
| 69 | + public function it_generates_an_fbp_when_the_request_has_none(): void |
| 70 | + { |
| 71 | + $resolvedCookies = (new CookieResolver())->resolve('www.example.com', [], []); |
| 72 | + |
| 73 | + self::assertNull($resolvedCookies->fbc); |
| 74 | + self::assertNotNull($resolvedCookies->fbp); |
| 75 | + self::assertSame(1, $resolvedCookies->fbp->getSubdomainIndex()); |
| 76 | + self::assertNotNull($resolvedCookies->fbp->getAppendix()); |
| 77 | + |
| 78 | + self::assertSame($resolvedCookies->fbp->value(), self::cookie($resolvedCookies, '_fbp')->value); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @test |
| 83 | + */ |
| 84 | + public function it_regenerates_the_fbp_when_the_existing_cookie_has_an_invalid_appendix(): void |
| 85 | + { |
| 86 | + // a two character appendix must be one of the language tokens Meta supports, so ZZ is invalid |
| 87 | + $resolvedCookies = (new CookieResolver())->resolve('www.example.com', [], ['_fbp' => 'fb.1.1656874832584.1088522659.ZZ']); |
| 88 | + |
| 89 | + self::assertNotNull($resolvedCookies->fbp); |
| 90 | + self::assertGreaterThan(1656874832584, $resolvedCookies->fbp->getCreationTime()); |
| 91 | + |
| 92 | + self::assertSame($resolvedCookies->fbp->value(), self::cookie($resolvedCookies, '_fbp')->value); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @test |
| 97 | + */ |
| 98 | + public function it_returns_null_for_a_cookie_that_cannot_be_represented_as_a_value_object(): void |
| 99 | + { |
| 100 | + // Meta's parameter builder only validates the segment count, so these pass through it, |
| 101 | + // but they are not valid fbc/fbp values |
| 102 | + $resolvedCookies = (new CookieResolver())->resolve('www.example.com', [], ['_fbc' => 'a.b.c.d', '_fbp' => 'e.f.g.h']); |
| 103 | + |
| 104 | + self::assertNull($resolvedCookies->fbc); |
| 105 | + self::assertNull($resolvedCookies->fbp); |
| 106 | + self::assertStringStartsWith('a.b.c.d.', self::cookie($resolvedCookies, '_fbc')->value); |
| 107 | + self::assertStringStartsWith('e.f.g.h.', self::cookie($resolvedCookies, '_fbp')->value); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @test |
| 112 | + */ |
| 113 | + public function it_uses_the_given_domains_to_derive_the_cookie_domain_and_subdomain_index(): void |
| 114 | + { |
| 115 | + $resolvedCookies = (new CookieResolver(['example.co.uk']))->resolve('shop.example.co.uk', [], []); |
| 116 | + |
| 117 | + self::assertNotNull($resolvedCookies->fbp); |
| 118 | + self::assertSame(2, $resolvedCookies->fbp->getSubdomainIndex()); |
| 119 | + self::assertSame('example.co.uk', self::cookie($resolvedCookies, '_fbp')->domain); |
| 120 | + } |
| 121 | + |
| 122 | + private static function cookie(ResolvedCookies $resolvedCookies, string $name): Cookie |
| 123 | + { |
| 124 | + foreach ($resolvedCookies->cookiesToSet as $cookie) { |
| 125 | + if ($cookie->name === $name) { |
| 126 | + return $cookie; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + self::fail(sprintf('No cookie named "%s" was set', $name)); |
| 131 | + } |
| 132 | +} |
0 commit comments