- Adds unit test for tracker router endpoint
This commit is contained in:
@@ -1,15 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Codeception\Util\Stub;
|
|
||||||
use MailPoet\Models\Newsletter;
|
use MailPoet\Models\Newsletter;
|
||||||
use MailPoet\Models\NewsletterLink;
|
use MailPoet\Models\NewsletterLink;
|
||||||
use MailPoet\Models\SendingQueue;
|
use MailPoet\Models\SendingQueue;
|
||||||
use MailPoet\Models\StatisticsClicks;
|
|
||||||
use MailPoet\Models\StatisticsOpens;
|
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
use MailPoet\Statistics\Track\Clicks;
|
use MailPoet\Router\Endpoints\Track;
|
||||||
|
|
||||||
class ClicksTest extends MailPoetTest {
|
class TrackTest extends MailPoetTest {
|
||||||
function _before() {
|
function _before() {
|
||||||
// create newsletter
|
// create newsletter
|
||||||
$newsletter = Newsletter::create();
|
$newsletter = Newsletter::create();
|
||||||
@@ -34,116 +31,84 @@ class ClicksTest extends MailPoetTest {
|
|||||||
$link->queue_id = $queue->id;
|
$link->queue_id = $queue->id;
|
||||||
$this->link = $link->save();
|
$this->link = $link->save();
|
||||||
// build track data
|
// build track data
|
||||||
$this->track_data = (object)array(
|
$this->track_data = array(
|
||||||
'queue' => $queue,
|
'queue_id' => $queue->id,
|
||||||
'subscriber' => $subscriber,
|
'subscriber_id' => $subscriber->id,
|
||||||
'newsletter' => $newsletter,
|
'newsletter_id' => $newsletter->id,
|
||||||
'subscriber_token' => Subscriber::generateToken($subscriber->email),
|
'subscriber_token' => Subscriber::generateToken($subscriber->email),
|
||||||
'link' => $link,
|
'link_hash' => $link->hash,
|
||||||
'preview' => false
|
'preview' => false
|
||||||
);
|
);
|
||||||
// instantiate class
|
|
||||||
$this->clicks = new Clicks();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItAbortsWhenTrackDataIsEmptyOrMissingLink() {
|
function testItReturnsFalseWhenTrackDataIsMissing() {
|
||||||
// abort function should be called twice:
|
// queue ID is required
|
||||||
$clicks = Stub::make($this->clicks, array(
|
|
||||||
'abort' => Stub::exactly(2, function() { })
|
|
||||||
), $this);
|
|
||||||
$data = $this->track_data;
|
$data = $this->track_data;
|
||||||
// 1. when tracking data does not exist
|
unset($data['queue_id']);
|
||||||
$clicks->track(false);
|
expect(Track::_processTrackData($data))->false();
|
||||||
// 2. when link model object is missing
|
// subscriber ID is required
|
||||||
unset($data->link);
|
$data = $this->track_data;
|
||||||
$clicks->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() {
|
function testItFailsWhenSubscriberTokenDoesNotMatch() {
|
||||||
$data = $this->track_data;
|
$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->subscriber->wp_user_id = 99;
|
||||||
$data->preview = true;
|
$data->preview = true;
|
||||||
$clicks = Stub::make($this->clicks, array(
|
expect(Track::_validateTrackData($data))->equals($data);
|
||||||
'redirectToUrl' => function() { }
|
|
||||||
), $this);
|
|
||||||
$clicks->track($data);
|
|
||||||
expect(StatisticsClicks::findMany())->isEmpty();
|
|
||||||
expect(StatisticsOpens::findMany())->isEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItTracksClickAndOpenEvent() {
|
function testItCanGetNewsletterFromQueue() {
|
||||||
$data = $this->track_data;
|
$data = $this->track_data;
|
||||||
$clicks = Stub::make($this->clicks, array(
|
$data['newsletter_id'] = false;
|
||||||
'redirectToUrl' => function() { }
|
$processed_data = Track::_processTrackData($this->track_data);
|
||||||
), $this);
|
expect($processed_data->newsletter->id)->equals($this->newsletter->id);
|
||||||
$clicks->track($data);
|
|
||||||
expect(StatisticsClicks::findMany())->notEmpty();
|
|
||||||
expect(StatisticsOpens::findMany())->notEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItRedirectsToUrlAfterTracking() {
|
function testItCanProcessTrackData() {
|
||||||
$clicks = Stub::make($this->clicks, array(
|
$processed_data = Track::_processTrackData($this->track_data);
|
||||||
'redirectToUrl' => Stub::exactly(1, function() { })
|
expect($processed_data->queue->id)->equals($this->queue->id);
|
||||||
), $this);
|
expect($processed_data->subscriber->id)->equals($this->subscriber->id);
|
||||||
$clicks->track($this->track_data);
|
expect($processed_data->newsletter->id)->equals($this->newsletter->id);
|
||||||
}
|
expect($processed_data->link->id)->equals($this->link->id);
|
||||||
|
|
||||||
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 _after() {
|
function _after() {
|
||||||
@@ -151,7 +116,5 @@ class ClicksTest extends MailPoetTest {
|
|||||||
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||||
ORM::raw_execute('TRUNCATE ' . NewsletterLink::$_table);
|
ORM::raw_execute('TRUNCATE ' . NewsletterLink::$_table);
|
||||||
ORM::raw_execute('TRUNCATE ' . SendingQueue::$_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