From 16cb91990b13a3cc45e72ba3cb15aa83baef389d Mon Sep 17 00:00:00 2001 From: Vlad Date: Tue, 31 May 2016 12:44:46 -0400 Subject: [PATCH] - Updates unit test --- lib/Newsletter/Shortcodes/Categories/Link.php | 4 +- .../Shortcodes/Categories/Subscriber.php | 3 +- tests/unit/Newsletter/ShortcodesTest.php | 38 ++++++++++++++++--- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/Newsletter/Shortcodes/Categories/Link.php b/lib/Newsletter/Shortcodes/Categories/Link.php index f22a425c4f..c26f987ec4 100644 --- a/lib/Newsletter/Shortcodes/Categories/Link.php +++ b/lib/Newsletter/Shortcodes/Categories/Link.php @@ -2,7 +2,7 @@ namespace MailPoet\Newsletter\Shortcodes\Categories; use MailPoet\Models\Setting; -use MailPoet\Models\Subscriber; +use MailPoet\Models\Subscriber as SubscriberModel; use MailPoet\Statistics\Track\Unsubscribes; use MailPoet\Subscription\Url as SubscriptionUrl; @@ -102,7 +102,7 @@ class Link { $subscriber['id'] : $subscriber, 'subscriber_token' => (isset($subscriber['id'])) ? - Subscriber::generateToken($subscriber['email']) : + SubscriberModel::generateToken($subscriber['email']) : false, 'queue' => (isset($queue['id'])) ? $queue['id'] : diff --git a/lib/Newsletter/Shortcodes/Categories/Subscriber.php b/lib/Newsletter/Shortcodes/Categories/Subscriber.php index dbbef54ebf..f533ea5ca6 100644 --- a/lib/Newsletter/Shortcodes/Categories/Subscriber.php +++ b/lib/Newsletter/Shortcodes/Categories/Subscriber.php @@ -2,6 +2,7 @@ namespace MailPoet\Newsletter\Shortcodes\Categories; use MailPoet\Models\SubscriberCustomField; +use MailPoet\Models\Subscriber as SubscriberModel; require_once(ABSPATH . 'wp-includes/pluggable.php'); @@ -30,7 +31,7 @@ class Subscriber { return $default_value; break; case 'count': - return Subscriber::filter('subscribed')->count(); + return SubscriberModel::filter('subscribed')->count(); break; case preg_match('/cf_(\d+)/', $action, $custom_field) ? true : false: if(empty($subscriber['id'])) return false; diff --git a/tests/unit/Newsletter/ShortcodesTest.php b/tests/unit/Newsletter/ShortcodesTest.php index 6dce3a37ca..50c394a1d4 100644 --- a/tests/unit/Newsletter/ShortcodesTest.php +++ b/tests/unit/Newsletter/ShortcodesTest.php @@ -1,9 +1,11 @@ equals(2); } - function testItCanProcessUserShortcodes() { + function testItCanProcessSubscriberShortcodes() { $shortcodes_object = $this->shortcodes_object; $result = - $shortcodes_object->process(array('[user:firstname]')); + $shortcodes_object->process(array('[subscriber:firstname]')); expect($result[0])->equals($this->subscriber->first_name); $result = - $shortcodes_object->process(array('[user:lastname]')); + $shortcodes_object->process(array('[subscriber:lastname]')); expect($result[0])->equals($this->subscriber->last_name); $result = - $shortcodes_object->process(array('[user:displayname]')); + $shortcodes_object->process(array('[subscriber:displayname]')); expect($result[0])->equals($this->WP_user->user_login); $subscribers = Subscriber::where('status', 'subscribed') ->findMany(); $subscriber_count = count($subscribers); $result = - $shortcodes_object->process(array('[user:count]')); + $shortcodes_object->process(array('[subscriber:count]')); expect($result[0])->equals($subscriber_count); $this->subscriber->status = 'unsubscribed'; $this->subscriber->save(); $result = - $shortcodes_object->process(array('[user:count]')); + $shortcodes_object->process(array('[subscriber:count]')); expect($result[0])->equals(--$subscriber_count); } + function testItCanProcessSubscriberCustomFieldShortcodes() { + $shortcodes_object = $this->shortcodes_object; + $subscriber = $this->subscriber; + $custom_field = CustomField::create(); + $custom_field->name = 'custom_field_name'; + $custom_field->type = 'text'; + $custom_field->save(); + $result = $shortcodes_object->process( + array('[subscriber:cf_' . $custom_field->id . ']') + ); + expect($result[0])->false(); + $subscriber_custom_field = SubscriberCustomField::create(); + $subscriber_custom_field->subscriber_id = $subscriber->id; + $subscriber_custom_field->custom_field_id = $custom_field->id; + $subscriber_custom_field->value = 'custom_field_value'; + $subscriber_custom_field->save(); + $result = $shortcodes_object->process( + array('[subscriber:cf_' . $custom_field->id . ']') + ); + expect($result[0])->equals($subscriber_custom_field->value); + } + function testItCanProcessLinkShortcodes() { $shortcodes_object = $this->shortcodes_object; $result = @@ -245,6 +269,8 @@ class ShortcodesTest extends MailPoetTest { function _after() { ORM::raw_execute('TRUNCATE ' . Subscriber::$_table); ORM::raw_execute('TRUNCATE ' . SendingQueue::$_table); + ORM::raw_execute('TRUNCATE ' . CustomField::$_table); + ORM::raw_execute('TRUNCATE ' . SubscriberCustomField::$_table); wp_delete_post($this->WP_post, true); wp_delete_user($this->WP_user->ID); }