- Adds unit test for tracker router endpoint
This commit is contained in:
@@ -1,15 +1,12 @@
|
||||
<?php
|
||||
|
||||
use Codeception\Util\Stub;
|
||||
use MailPoet\Models\Newsletter;
|
||||
use MailPoet\Models\NewsletterLink;
|
||||
use MailPoet\Models\SendingQueue;
|
||||
use MailPoet\Models\StatisticsClicks;
|
||||
use MailPoet\Models\StatisticsOpens;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Statistics\Track\Clicks;
|
||||
use MailPoet\Router\Endpoints\Track;
|
||||
|
||||
class ClicksTest extends MailPoetTest {
|
||||
class TrackTest extends MailPoetTest {
|
||||
function _before() {
|
||||
// create newsletter
|
||||
$newsletter = Newsletter::create();
|
||||
@@ -34,116 +31,84 @@ class ClicksTest extends MailPoetTest {
|
||||
$link->queue_id = $queue->id;
|
||||
$this->link = $link->save();
|
||||
// build track data
|
||||
$this->track_data = (object)array(
|
||||
'queue' => $queue,
|
||||
'subscriber' => $subscriber,
|
||||
'newsletter' => $newsletter,
|
||||
$this->track_data = array(
|
||||
'queue_id' => $queue->id,
|
||||
'subscriber_id' => $subscriber->id,
|
||||
'newsletter_id' => $newsletter->id,
|
||||
'subscriber_token' => Subscriber::generateToken($subscriber->email),
|
||||
'link' => $link,
|
||||
'link_hash' => $link->hash,
|
||||
'preview' => false
|
||||
);
|
||||
// instantiate class
|
||||
$this->clicks = new Clicks();
|
||||
}
|
||||
|
||||
function testItAbortsWhenTrackDataIsEmptyOrMissingLink() {
|
||||
// abort function should be called twice:
|
||||
$clicks = Stub::make($this->clicks, array(
|
||||
'abort' => Stub::exactly(2, function() { })
|
||||
), $this);
|
||||
function testItReturnsFalseWhenTrackDataIsMissing() {
|
||||
// queue ID is required
|
||||
$data = $this->track_data;
|
||||
// 1. when tracking data does not exist
|
||||
$clicks->track(false);
|
||||
// 2. when link model object is missing
|
||||
unset($data->link);
|
||||
$clicks->track($data);
|
||||
unset($data['queue_id']);
|
||||
expect(Track::_processTrackData($data))->false();
|
||||
// subscriber ID is required
|
||||
$data = $this->track_data;
|
||||
unset($data['subscriber_id']);
|
||||
expect(Track::_processTrackData($data))->false();
|
||||
// subscriber token is required
|
||||
$data = $this->track_data;
|
||||
unset($data['subscriber_token']);
|
||||
expect(Track::_processTrackData($data))->false();
|
||||
}
|
||||
|
||||
function testItDoesNotTrackEventsFromWpUserWhenPreviewIsEnabled() {
|
||||
$data = $this->track_data;
|
||||
function testItFailsWhenSubscriberTokenDoesNotMatch() {
|
||||
$data = (object)array_merge(
|
||||
$this->track_data,
|
||||
array(
|
||||
'queue' => $this->queue,
|
||||
'subscriber' => $this->subscriber,
|
||||
'newsletter' => $this->newsletter
|
||||
)
|
||||
);
|
||||
$data->subscriber->email = 'random@email.com';
|
||||
expect(Track::_validateTrackData($data))->false();
|
||||
}
|
||||
|
||||
function testItFailsWhenSubscriberIsNotOnProcessedList() {
|
||||
$data = (object)array_merge(
|
||||
$this->track_data,
|
||||
array(
|
||||
'queue' => $this->queue,
|
||||
'subscriber' => $this->subscriber,
|
||||
'newsletter' => $this->newsletter
|
||||
)
|
||||
);
|
||||
$data->subscriber->id = 99;
|
||||
expect(Track::_validateTrackData($data))->false();
|
||||
}
|
||||
|
||||
function testItDoesNotRequireWpUsersToBeOnProcessedListWhenPreviewIsEnabled() {
|
||||
$data = (object)array_merge(
|
||||
$this->track_data,
|
||||
array(
|
||||
'queue' => $this->queue,
|
||||
'subscriber' => $this->subscriber,
|
||||
'newsletter' => $this->newsletter
|
||||
)
|
||||
);
|
||||
$data->subscriber->wp_user_id = 99;
|
||||
$data->preview = true;
|
||||
$clicks = Stub::make($this->clicks, array(
|
||||
'redirectToUrl' => function() { }
|
||||
), $this);
|
||||
$clicks->track($data);
|
||||
expect(StatisticsClicks::findMany())->isEmpty();
|
||||
expect(StatisticsOpens::findMany())->isEmpty();
|
||||
expect(Track::_validateTrackData($data))->equals($data);
|
||||
}
|
||||
|
||||
function testItTracksClickAndOpenEvent() {
|
||||
function testItCanGetNewsletterFromQueue() {
|
||||
$data = $this->track_data;
|
||||
$clicks = Stub::make($this->clicks, array(
|
||||
'redirectToUrl' => function() { }
|
||||
), $this);
|
||||
$clicks->track($data);
|
||||
expect(StatisticsClicks::findMany())->notEmpty();
|
||||
expect(StatisticsOpens::findMany())->notEmpty();
|
||||
$data['newsletter_id'] = false;
|
||||
$processed_data = Track::_processTrackData($this->track_data);
|
||||
expect($processed_data->newsletter->id)->equals($this->newsletter->id);
|
||||
}
|
||||
|
||||
function testItRedirectsToUrlAfterTracking() {
|
||||
$clicks = Stub::make($this->clicks, array(
|
||||
'redirectToUrl' => Stub::exactly(1, function() { })
|
||||
), $this);
|
||||
$clicks->track($this->track_data);
|
||||
}
|
||||
|
||||
function testItIncrementsClickEventCount() {
|
||||
$clicks = Stub::make($this->clicks, array(
|
||||
'redirectToUrl' => function() { }
|
||||
), $this);
|
||||
$clicks->track($this->track_data);
|
||||
expect(StatisticsClicks::findMany()[0]->count)->equals(1);
|
||||
$clicks->track($this->track_data);
|
||||
expect(StatisticsClicks::findMany()[0]->count)->equals(2);
|
||||
}
|
||||
|
||||
function testItConvertsShortcodesToUrl() {
|
||||
$link = $this->clicks->processUrl(
|
||||
'[link:newsletter_view_in_browser_url]',
|
||||
$this->newsletter,
|
||||
$this->subscriber,
|
||||
$this->queue,
|
||||
$preview = false
|
||||
);
|
||||
expect($link)->contains('&endpoint=view_in_browser');
|
||||
}
|
||||
|
||||
function testItFailsToConvertsInvalidShortcodeToUrl() {
|
||||
$clicks = Stub::make($this->clicks, array(
|
||||
'abort' => Stub::exactly(1, function() { })
|
||||
), $this);
|
||||
// should call abort() method if shortcode action does not exist
|
||||
$link = $clicks->processUrl(
|
||||
'[link:]',
|
||||
$this->newsletter,
|
||||
$this->subscriber,
|
||||
$this->queue,
|
||||
$preview = false
|
||||
);
|
||||
}
|
||||
|
||||
function testItDoesNotConvertNonexistentShortcodeToUrl() {
|
||||
$link = $this->clicks->processUrl(
|
||||
'[link:unknown_shortcode]',
|
||||
$this->newsletter,
|
||||
$this->subscriber,
|
||||
$this->queue,
|
||||
$preview = false
|
||||
);
|
||||
expect($link)->equals('[link:unknown_shortcode]');
|
||||
}
|
||||
|
||||
|
||||
function testItDoesNotConvertRegulaUrls() {
|
||||
$link = $this->clicks->processUrl(
|
||||
'http://example.com',
|
||||
$this->newsletter,
|
||||
$this->subscriber,
|
||||
$this->queue,
|
||||
$preview = false
|
||||
);
|
||||
expect($link)->equals('http://example.com');
|
||||
function testItCanProcessTrackData() {
|
||||
$processed_data = Track::_processTrackData($this->track_data);
|
||||
expect($processed_data->queue->id)->equals($this->queue->id);
|
||||
expect($processed_data->subscriber->id)->equals($this->subscriber->id);
|
||||
expect($processed_data->newsletter->id)->equals($this->newsletter->id);
|
||||
expect($processed_data->link->id)->equals($this->link->id);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
@@ -151,7 +116,5 @@ class ClicksTest extends MailPoetTest {
|
||||
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||
ORM::raw_execute('TRUNCATE ' . NewsletterLink::$_table);
|
||||
ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table);
|
||||
ORM::raw_execute('TRUNCATE ' . StatisticsOpens::$_table);
|
||||
ORM::raw_execute('TRUNCATE ' . StatisticsClicks::$_table);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user