|
| 1 | +// |
| 2 | +// MPKitFirebaseGA4SwiftTests.swift |
| 3 | +// mParticle-Google-Analytics-Firebase-GA4 |
| 4 | +// |
| 5 | +// Created by Nick Dimitrakas on 9/12/25. |
| 6 | +// |
| 7 | + |
| 8 | +import XCTest |
| 9 | +@testable import mParticle_Google_Analytics_Firebase_GA4 |
| 10 | + |
| 11 | +final class MPKitFirebaseGA4AnalyticsTests: XCTestCase { |
| 12 | + var kit: MPKitFirebaseGA4Analytics! |
| 13 | + |
| 14 | + override func setUp() { |
| 15 | + super.setUp() |
| 16 | + kit = MPKitFirebaseGA4Analytics() |
| 17 | + kit.configuration = [:] |
| 18 | + } |
| 19 | + |
| 20 | + override func tearDown() { |
| 21 | + kit = nil |
| 22 | + super.tearDown() |
| 23 | + } |
| 24 | + |
| 25 | + // MARK: - resolvedConsentForPurpose |
| 26 | + |
| 27 | + func testResolvedConsentForPurpose_whenConsentGranted_returnsYES() { |
| 28 | + let consent = MPGDPRConsent() |
| 29 | + consent.consented = true |
| 30 | + let gdprConsents = ["analytics": consent] |
| 31 | + |
| 32 | + let result = kit.resolvedConsent(forPurpose: "analytics", gdprConsents: gdprConsents) |
| 33 | + |
| 34 | + XCTAssertEqual(result, true) |
| 35 | + } |
| 36 | + |
| 37 | + func testResolvedConsentForPurpose_whenConsentDenied_returnsNO() { |
| 38 | + let consent = MPGDPRConsent() |
| 39 | + consent.consented = false |
| 40 | + let gdprConsents = ["ads": consent] |
| 41 | + |
| 42 | + let result = kit.resolvedConsent(forPurpose: "ads", gdprConsents: gdprConsents) |
| 43 | + |
| 44 | + XCTAssertEqual(result, false) |
| 45 | + } |
| 46 | + |
| 47 | + func testResolvedConsentForPurpose_whenConsentMissing_returnsNil() { |
| 48 | + let gdprConsents: [String: MPGDPRConsent] = [:] |
| 49 | + |
| 50 | + let result = kit.resolvedConsent(forPurpose: "ads", gdprConsents: gdprConsents) |
| 51 | + |
| 52 | + XCTAssertNil(result) |
| 53 | + } |
| 54 | + |
| 55 | + // MARK: - resolvedConsentFromDefault |
| 56 | + |
| 57 | + func testResolvedConsentFromDefault_whenGranted_returnsYES() { |
| 58 | + kit.configuration = ["purpose_default": "Granted"] |
| 59 | + |
| 60 | + let result = kit.resolvedConsent(fromDefault: "purpose_default") |
| 61 | + |
| 62 | + XCTAssertEqual(result, true) |
| 63 | + } |
| 64 | + |
| 65 | + func testResolvedConsentFromDefault_whenDenied_returnsNO() { |
| 66 | + kit.configuration = ["purpose_default": "Denied"] |
| 67 | + |
| 68 | + let result = kit.resolvedConsent(fromDefault: "purpose_default") |
| 69 | + |
| 70 | + XCTAssertEqual(result, false) |
| 71 | + } |
| 72 | + |
| 73 | + func testResolvedConsentFromDefault_whenMissing_returnsNil() { |
| 74 | + kit.configuration = [:] |
| 75 | + |
| 76 | + let result = kit.resolvedConsent(fromDefault: "purpose_default") |
| 77 | + |
| 78 | + XCTAssertNil(result) |
| 79 | + } |
| 80 | + |
| 81 | + // MARK: - resolvedConsentForMappingKey |
| 82 | + |
| 83 | + func testResolvedConsentForMappingKey_prefersGDPRConsentOverDefault() { |
| 84 | + let consent = MPGDPRConsent() |
| 85 | + consent.consented = true |
| 86 | + let gdprConsents = ["analytics": consent] |
| 87 | + |
| 88 | + let mapping = ["tracking": "analytics"] |
| 89 | + kit.configuration = ["tracking_default": "Denied"] |
| 90 | + |
| 91 | + let result = kit.resolvedConsent(forMappingKey: "tracking", |
| 92 | + defaultKey: "tracking_default", |
| 93 | + gdprConsents: gdprConsents, |
| 94 | + mapping: mapping) |
| 95 | + |
| 96 | + // Should return GDPR (true) instead of config (Denied/false) |
| 97 | + XCTAssertEqual(result, true) |
| 98 | + } |
| 99 | + |
| 100 | + func testResolvedConsentForMappingKey_fallsBackToDefaultWhenNoGDPR() { |
| 101 | + let gdprConsents: [String: MPGDPRConsent] = [:] |
| 102 | + let mapping = ["tracking": "ads"] |
| 103 | + kit.configuration = ["tracking_default": "Granted"] |
| 104 | + |
| 105 | + let result = kit.resolvedConsent(forMappingKey: "tracking", |
| 106 | + defaultKey: "tracking_default", |
| 107 | + gdprConsents: gdprConsents, |
| 108 | + mapping: mapping) |
| 109 | + |
| 110 | + XCTAssertEqual(result, true) |
| 111 | + } |
| 112 | + |
| 113 | + func testResolvedConsentForMappingKey_returnsNilWhenNeitherFound() { |
| 114 | + let gdprConsents: [String: MPGDPRConsent] = [:] |
| 115 | + let mapping = ["tracking": "ads"] |
| 116 | + kit.configuration = [:] |
| 117 | + |
| 118 | + let result = kit.resolvedConsent(forMappingKey: "tracking", |
| 119 | + defaultKey: "tracking_default", |
| 120 | + gdprConsents: gdprConsents, |
| 121 | + mapping: mapping) |
| 122 | + |
| 123 | + XCTAssertNil(result) |
| 124 | + } |
| 125 | +} |
0 commit comments