- Updates unit test

This commit is contained in:
Vlad
2016-05-02 13:44:36 -04:00
parent 3213dd0d08
commit f36dbb78e6

View File

@ -47,32 +47,34 @@ class ShortcodesTest extends MailPoetTest {
} }
function testItCanMatchShortcodeDetails() { function testItCanMatchShortcodeDetails() {
$shortcodes_object = $this->shortcodes_object;
$content = '[category:action]'; $content = '[category:action]';
$details = $this->shortcodes_object->match($content); $details = $shortcodes_object->match($content);
expect($details['category'])->equals('category'); expect($details['category'])->equals('category');
expect($details['action'])->equals('action'); expect($details['action'])->equals('action');
$content = '[category:action|default:default_value]'; $content = '[category:action|default:default_value]';
$details = $this->shortcodes_object->match($content); $details = $shortcodes_object->match($content);
expect($details['category'])->equals('category'); expect($details['category'])->equals('category');
expect($details['action'])->equals('action'); expect($details['action'])->equals('action');
expect($details['default'])->equals('default_value'); expect($details['default'])->equals('default_value');
$content = '[category:action|default]'; $content = '[category:action|default]';
$details = $this->shortcodes_object->match($content); $details = $shortcodes_object->match($content);
expect($details)->isEmpty(); expect($details)->isEmpty();
$content = '[category|default:default_value]'; $content = '[category|default:default_value]';
$details = $this->shortcodes_object->match($content); $details = $shortcodes_object->match($content);
expect($details)->isEmpty(); expect($details)->isEmpty();
} }
function testItCanProcessCustomShortcodes() { function testItCanProcessCustomShortcodes() {
$shortcodes_object = $this->shortcodes_object;
$shortcode = array('[some:shortcode]'); $shortcode = array('[some:shortcode]');
$result = $this->shortcodes_object->process($shortcode); $result = $shortcodes_object->process($shortcode);
expect($result[0])->false(); expect($result[0])->false();
add_filter('mailpoet_newsletter_shortcode', function ( add_filter('mailpoet_newsletter_shortcode', function (
$shortcode, $newsletter, $subscriber, $queue, $content) { $shortcode, $newsletter, $subscriber, $queue, $content) {
if($shortcode === '[some:shortcode]') return 'success'; if($shortcode === '[some:shortcode]') return 'success';
}, 10, 5); }, 10, 5);
$result = $this->shortcodes_object->process($shortcode); $result = $shortcodes_object->process($shortcode);
expect($result[0])->equals('success'); expect($result[0])->equals('success');
} }
@ -87,80 +89,84 @@ class ShortcodesTest extends MailPoetTest {
} }
function testItCanProcessNewsletterShortcodes() { function testItCanProcessNewsletterShortcodes() {
$shortcodes_object = $this->shortcodes_object;
$content = $content =
'<a data-post-id="' . $this->WP_post . '" href="#">latest post</a>' . '<a data-post-id="' . $this->WP_post . '" href="#">latest post</a>' .
'<a data-post-id="10" href="#">another post</a>' . '<a data-post-id="10" href="#">another post</a>' .
'<a href="#">not post</a>'; '<a href="#">not post</a>';
$result = $result =
$this->shortcodes_object->process(array('[newsletter:subject]')); $shortcodes_object->process(array('[newsletter:subject]'));
expect($result[0])->equals($this->newsletter['subject']); expect($result[0])->equals($this->newsletter['subject']);
$result = $result =
$this->shortcodes_object->process(array('[newsletter:total]'), $content); $shortcodes_object->process(array('[newsletter:total]'), $content);
expect($result[0])->equals(2); expect($result[0])->equals(2);
$result = $result =
$this->shortcodes_object->process(array('[newsletter:post_title]')); $shortcodes_object->process(array('[newsletter:post_title]'));
$wp_post = get_post($this->WP_post); $wp_post = get_post($this->WP_post);
expect($result['0'])->equals($wp_post->post_title); expect($result['0'])->equals($wp_post->post_title);
$result = $result =
$this->shortcodes_object->process(array('[newsletter:number]')); $shortcodes_object->process(array('[newsletter:number]'));
expect($result['0'])->equals(1); expect($result['0'])->equals(1);
$queue = $this->_createQueue(); $queue = $this->_createQueue();
$result = $result =
$this->shortcodes_object->process(array('[newsletter:number]')); $shortcodes_object->process(array('[newsletter:number]'));
expect($result['0'])->equals(2); expect($result['0'])->equals(2);
} }
function testItCanProcessUserShortcodes() { function testItCanProcessUserShortcodes() {
$shortcodes_object = $this->shortcodes_object;
$result = $result =
$this->shortcodes_object->process(array('[user:firstname]')); $shortcodes_object->process(array('[user:firstname]'));
expect($result[0])->equals($this->subscriber->first_name); expect($result[0])->equals($this->subscriber->first_name);
$result = $result =
$this->shortcodes_object->process(array('[user:lastname]')); $shortcodes_object->process(array('[user:lastname]'));
expect($result[0])->equals($this->subscriber->last_name); expect($result[0])->equals($this->subscriber->last_name);
$result = $result =
$this->shortcodes_object->process(array('[user:displayname]')); $shortcodes_object->process(array('[user:displayname]'));
expect($result[0])->equals($this->WP_user->user_login); expect($result[0])->equals($this->WP_user->user_login);
$subscribers = Subscriber::where('status', 'subscribed') $subscribers = Subscriber::where('status', 'subscribed')
->findMany(); ->findMany();
$subscriber_count = count($subscribers); $subscriber_count = count($subscribers);
$result = $result =
$this->shortcodes_object->process(array('[user:count]')); $shortcodes_object->process(array('[user:count]'));
expect($result[0])->equals($subscriber_count); expect($result[0])->equals($subscriber_count);
$this->subscriber->status = 'unsubscribed'; $this->subscriber->status = 'unsubscribed';
$this->subscriber->save(); $this->subscriber->save();
$result = $result =
$this->shortcodes_object->process(array('[user:count]')); $shortcodes_object->process(array('[user:count]'));
expect($result[0])->equals(--$subscriber_count); expect($result[0])->equals(--$subscriber_count);
} }
function testItCanProcessLinkShortcodes() { function testItCanProcessLinkShortcodes() {
$shortcodes_object = $this->shortcodes_object;
$result = $result =
$this->shortcodes_object->process(array('[link:subscription_unsubscribe]')); $shortcodes_object->process(array('[link:subscription_unsubscribe]'));
expect(preg_match('/^<a.*?\/a>$/', $result['0']))->equals(1); expect(preg_match('/^<a.*?\/a>$/', $result['0']))->equals(1);
expect(preg_match('/action=unsubscribe/', $result['0']))->equals(1); expect(preg_match('/action=unsubscribe/', $result['0']))->equals(1);
$result = $result =
$this->shortcodes_object->process(array('[link:subscription_unsubscribe_url]')); $shortcodes_object->process(array('[link:subscription_unsubscribe_url]'));
expect(preg_match('/^http.*?action=unsubscribe/', $result['0']))->equals(1); expect(preg_match('/^http.*?action=unsubscribe/', $result['0']))->equals(1);
$result = $result =
$this->shortcodes_object->process(array('[link:subscription_manage]')); $shortcodes_object->process(array('[link:subscription_manage]'));
expect(preg_match('/^<a.*?\/a>$/', $result['0']))->equals(1); expect(preg_match('/^<a.*?\/a>$/', $result['0']))->equals(1);
expect(preg_match('/action=manage/', $result['0']))->equals(1); expect(preg_match('/action=manage/', $result['0']))->equals(1);
$result = $result =
$this->shortcodes_object->process(array('[link:subscription_manage_url]')); $shortcodes_object->process(array('[link:subscription_manage_url]'));
expect(preg_match('/^http.*?action=manage/', $result['0']))->equals(1); expect(preg_match('/^http.*?action=manage/', $result['0']))->equals(1);
$result = $result =
$this->shortcodes_object->process(array('[link:newsletter_view_in_browser]')); $shortcodes_object->process(array('[link:newsletter_view_in_browser]'));
expect(preg_match('/^<a.*?\/a>$/', $result['0']))->equals(1); expect(preg_match('/^<a.*?\/a>$/', $result['0']))->equals(1);
expect(preg_match('/endpoint=view_in_browser/', $result['0']))->equals(1); expect(preg_match('/endpoint=view_in_browser/', $result['0']))->equals(1);
$result = $result =
$this->shortcodes_object->process(array('[link:newsletter_view_in_browser_url]')); $shortcodes_object->process(array('[link:newsletter_view_in_browser_url]'));
expect(preg_match('/^http.*?endpoint=view_in_browser/', $result['0']))->equals(1); expect(preg_match('/^http.*?endpoint=view_in_browser/', $result['0']))->equals(1);
} }
function testItReturnsShortcodeWhenTrackingEnabled() { function testItReturnsShortcodeWhenTrackingEnabled() {
$shortcodes_object = $this->shortcodes_object;
$shortcode = '[link:subscription_unsubscribe_url]'; $shortcode = '[link:subscription_unsubscribe_url]';
$result = $result =
$this->shortcodes_object->process(array($shortcode)); $shortcodes_object->process(array($shortcode));
expect(preg_match('/^http.*?action=unsubscribe/', $result['0']))->equals(1); expect(preg_match('/^http.*?action=unsubscribe/', $result['0']))->equals(1);
Setting::setValue('tracking.enabled', true); Setting::setValue('tracking.enabled', true);
$shortcodes = array( $shortcodes = array(
@ -171,25 +177,30 @@ class ShortcodesTest extends MailPoetTest {
'[link:newsletter_view_in_browser]', '[link:newsletter_view_in_browser]',
'[link:newsletter_view_in_browser_url]' '[link:newsletter_view_in_browser_url]'
); );
// tracking function only works during sending, so queue object must not be false
$shortcodes_object->queue = true;
$result = $result =
$this->shortcodes_object->process($shortcodes); $shortcodes_object->process($shortcodes);
// all returned shortcodes must end with url // all returned shortcodes must end with url
$result = join(',', $result); $result = join(',', $result);
expect(substr_count($result, '_url'))->equals(count($shortcodes)); expect(substr_count($result, '_url'))->equals(count($shortcodes));
} }
function testItCanProcessCustomLinkShortcodes() { function testItCanProcessCustomLinkShortcodes() {
$shortcodes_object = $this->shortcodes_object;
$shortcode = '[link:shortcode]'; $shortcode = '[link:shortcode]';
$result = $this->shortcodes_object->process(array($shortcode)); $result = $shortcodes_object->process(array($shortcode));
expect($result[0])->false(); expect($result[0])->false();
add_filter('mailpoet_newsletter_shortcode_link', function ( add_filter('mailpoet_newsletter_shortcode_link', function (
$shortcode, $newsletter, $subscriber, $queue) { $shortcode, $newsletter, $subscriber, $queue) {
if($shortcode === '[link:shortcode]') return 'success'; if($shortcode === '[link:shortcode]') return 'success';
}, 10, 4); }, 10, 4);
$result = $this->shortcodes_object->process(array($shortcode)); $result = $shortcodes_object->process(array($shortcode));
expect($result[0])->equals('success'); expect($result[0])->equals('success');
Setting::setValue('tracking.enabled', true); Setting::setValue('tracking.enabled', true);
$result = $this->shortcodes_object->process(array($shortcode)); // tracking function only works during sending, so queue object must not be false
$shortcodes_object->queue = true;
$result = $shortcodes_object->process(array($shortcode));
expect($result[0])->equals($shortcode); expect($result[0])->equals($shortcode);
} }