diff --git a/lib/Referrals/UrlDecorator.php b/lib/Referrals/UrlDecorator.php new file mode 100644 index 0000000000..bc7bdaa804 --- /dev/null +++ b/lib/Referrals/UrlDecorator.php @@ -0,0 +1,30 @@ +wp = $wp; + $this->settings = $settings; + } + + function decorate($url) { + $referral_id = $this->settings->get(ReferralDetector::REFERRAL_SETTING_NAME, null); + if ($referral_id === null) { + return $url; + } + return $this->wp->addQueryArg('ref', $referral_id, $url); + } +} diff --git a/tests/integration/Referrals/UrlDecoratorTest.php b/tests/integration/Referrals/UrlDecoratorTest.php new file mode 100644 index 0000000000..34a7c27c4d --- /dev/null +++ b/tests/integration/Referrals/UrlDecoratorTest.php @@ -0,0 +1,37 @@ +settings = new SettingsController(); + $this->url_decorator = new UrlDecorator(WPFunctions::get(), $this->settings); + } + + function testItDoesntDoAnythingWhenNoReferralId() { + $this->settings->set(ReferralDetector::REFERRAL_SETTING_NAME, null); + $url = 'http://example.com'; + expect($this->url_decorator->decorate($url))->equals($url); + } + + function testItCorrectlyAddsReferralId() { + $this->settings->set(ReferralDetector::REFERRAL_SETTING_NAME, 'abcdefgh'); + expect($this->url_decorator->decorate('http://example.com/')) + ->equals('http://example.com/?ref=abcdefgh'); + expect($this->url_decorator->decorate('http://example.com/?param=value')) + ->equals('http://example.com/?param=value&ref=abcdefgh'); + expect($this->url_decorator->decorate('http://example.com/?param=value#hash/?param=val')) + ->equals('http://example.com/?param=value&ref=abcdefgh#hash/?param=val'); + } +}