File size: 3,564 Bytes
6773345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php

namespace Tests\Unit;

use App\Domain\Applications\ApplicationService;
use ReflectionClass;
use Tests\TestCase;

class CalendlyConfigurationTest extends TestCase
{
    public function test_vetting_booking_url_uses_event_specific_config(): void
    {
        config([
            'app.calendly_booking_url' => 'https://calendly.com/legacy/general',
            'app.calendly_booking_urls.vetting' => 'https://calendly.com/mdn/vetting',
        ]);

        $this->assertSame('https://calendly.com/mdn/vetting', $this->bookingUrl('vetting'));
    }

    public function test_vetting_booking_url_falls_back_to_legacy_config(): void
    {
        config([
            'app.calendly_booking_url' => 'https://calendly.com/legacy/general',
            'app.calendly_booking_urls.vetting' => null,
        ]);

        $this->assertSame('https://calendly.com/legacy/general', $this->bookingUrl('vetting'));
    }

    public function test_vetting_booking_url_can_be_overridden_per_request(): void
    {
        config([
            'app.calendly_booking_url' => 'https://calendly.com/legacy/general',
            'app.calendly_booking_urls.vetting' => 'https://calendly.com/mdn/vetting',
        ]);

        $this->assertSame('https://calendly.com/custom/vetting', $this->bookingUrl('vetting', 'https://calendly.com/custom/vetting'));
    }

    public function test_tracked_booking_url_includes_application_and_vetting_identifiers(): void
    {
        $url = $this->trackedBookingUrl('https://calendly.com/mdn/vetting?month=2026-07', 'app-123', 'vetting-456');

        $this->assertStringStartsWith('https://calendly.com/mdn/vetting?month=2026-07&', $url);
        $this->assertStringContainsString('utm_content=app-123', $url);
        $this->assertStringContainsString('utm_term=vetting-456', $url);
        $this->assertStringContainsString('utm_campaign=application_vetting', $url);
    }

    public function test_tracking_values_are_read_from_calendly_payload(): void
    {
        $payload = [
            'tracking' => [
                'utm_content' => 'app-123',
                'utm_term' => 'vetting-456',
            ],
        ];

        $this->assertSame('app-123', $this->trackingValue($payload, 'utm_content'));
        $this->assertSame('vetting-456', $this->trackingValue($payload, 'utm_term'));
    }

    private function bookingUrl(string $eventType, ?string $override = null): ?string
    {
        $reflection = new ReflectionClass(ApplicationService::class);
        $service = $reflection->newInstanceWithoutConstructor();
        $method = $reflection->getMethod('calendlyBookingUrl');
        $method->setAccessible(true);

        return $method->invoke($service, $eventType, $override);
    }

    private function trackedBookingUrl(string $bookingUrl, string $applicationId, string $vettingId): string
    {
        $reflection = new ReflectionClass(ApplicationService::class);
        $service = $reflection->newInstanceWithoutConstructor();
        $method = $reflection->getMethod('calendlyTrackedBookingUrl');
        $method->setAccessible(true);

        return $method->invoke($service, $bookingUrl, $applicationId, $vettingId);
    }

    private function trackingValue(array $payload, string $key): ?string
    {
        $reflection = new ReflectionClass(ApplicationService::class);
        $service = $reflection->newInstanceWithoutConstructor();
        $method = $reflection->getMethod('calendlyTrackingValue');
        $method->setAccessible(true);

        return $method->invoke($service, $payload, $key);
    }
}