From b68ae3a23c35340b3415be477178aa9165732e6b Mon Sep 17 00:00:00 2001
From: Bulat <byapparov@gmail.com>
Date: Sun, 12 May 2013 10:17:26 +0100
Subject: [PATCH 1/2] Added initial test cases with base class and first test
 working test case for subscriptions_content.

---
 subscriptions.info         |   3 ++
 subscriptions.test         |  61 +++++++++++++++++++++
 subscriptions_content.test | 130 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 194 insertions(+)
 create mode 100644 subscriptions.test
 create mode 100644 subscriptions_content.test

diff --git a/subscriptions.info b/subscriptions.info
index 2e0bef0..4a7b4d5 100644
--- a/subscriptions.info
+++ b/subscriptions.info
@@ -4,3 +4,6 @@ package = "Subscriptions"
 core = 7.x
 
 configure = admin/config/system/subscriptions
+
+files[] = subscriptions.test
+files[] = subscriptions_content.test
\ No newline at end of file
diff --git a/subscriptions.test b/subscriptions.test
new file mode 100644
index 0000000..22062d9
--- /dev/null
+++ b/subscriptions.test
@@ -0,0 +1,61 @@
+<?php
+/**
+ * @file
+ * Contains base class for test cases for Subscriptions module
+ */
+
+/**
+ * Base test case class for Subscriptions
+ */
+class SubscriptionsTestCase extends DrupalWebTestCase {
+  function setUp() {
+    $modules = array_merge(array('subscriptions'), func_get_args());
+    parent::setUp($modules);
+  }
+  
+  /**
+   * Gets records from subscriptions_queue table
+   * @param array $conditions
+   * @return array queue items - fetched records from queue table
+   */
+  protected function getSubscriptionQueueItems($conditions = array()) {
+     $query = db_select('subscriptions_queue', 'sq')
+      ->fields('sq', array('sqid', 'value', 'field', 'uid', 'load_args',));
+     foreach ($conditions as $field => $value) {
+       $query->condition($field, $value);
+     }
+     $queue = $query->execute()->fetchAll();
+     return $queue;
+  } 
+  
+  /**
+   * Asserts number of subscription that exist for specificed parameter for user
+   */
+  protected function assertSubscriptions($params, $uid, $count, $message = '') {
+    $s = subscriptions_get($params);
+    if ($count == 0) {
+      // check that user has no subscriptions
+      $this->assertFalse(isset($s[$uid]), $message ? $message : t('User has no subscriptions.'));
+    } 
+    else {
+      // check if that and only item is in the queue (uA is autosubscribed)
+      $this->assert(isset($s[$uid]) && count($s[$uid]) == $count, $message ? $message : t('User has @count subscriptions.', array('@count' => $count)));
+    }
+  }
+  
+  /**
+   * Asserts number of items in subscription queue for specified conditions
+   */
+  protected function assertSubscriptionsQueue($conditions, $items, $message ='') {
+    $queue = $this->getSubscriptionQueueItems($conditions);
+    if ($items == 0) {
+      $this->assertFalse($queue, $message ? $message : t('Subscriptions queue is empty for provided conditions.'));
+    }
+    else {
+      $this->assertEqual(count($queue), $items, $message ? $message : t('@items items found in subscriptions queue.', array('@items' => $items)));
+    }
+  }
+  
+}
+
+
diff --git a/subscriptions_content.test b/subscriptions_content.test
new file mode 100644
index 0000000..5c33e24
--- /dev/null
+++ b/subscriptions_content.test
@@ -0,0 +1,130 @@
+<?php
+
+/**
+ * @file
+ * Contains test case for Subscriptions Content module.
+ */
+
+/** 
+ * Class with tests for Subscriptions Content module.
+ */
+class SubscriptionsContnentTestCase extends SubscriptionsTestCase {
+  protected $node;
+  protected $user_a;
+  protected $user_b;
+  
+  public static function getInfo() {
+    return array(
+      'name' => 'Subscription Conentent Drupal Web Test',
+      'description' => 'Drupal Web tests for subscriptions conent.',
+      'group' => 'Subscriptions',
+    );
+  }
+  
+  function setUp() {
+    parent::setUp('subscriptions_content');
+    
+    // create user A (uA)
+    $this->user_a = $this->drupalCreateUser(array(
+      'subscribe to content types',
+    ));
+    // We need to set access attribute on account.
+    $this->drupalLogin($this->user_a); 
+    
+    // create user B (uB)
+    $this->user_b = $this->drupalCreateUser(array(
+      'subscribe to content types',
+    ));
+    // We need to set access attribute on account.
+    $this->drupalLogin($this->user_b); 
+    $this->drupalLogout();
+    
+    // Create a node for testing.
+    $settings = array(
+      'type' => 'page',
+      'uid' => $this->user_a->uid,
+    );
+    $this->node = $this->drupalCreateNode($settings);
+  }
+
+
+  /**
+   * Tests subscribe functions used by rules
+   * @see subscriptions_write_subscription()
+   * 
+   */
+  function testWriteSubsciptionFunction() {
+    $params = array( // default pararms for getting our subscriptions
+      'module' => 'node', 
+      'field' => 'type',
+      'value' => 'page');
+    
+    // subscribe uA to nP : Authors are not auto subscribed see : rules_subscribe_author_to_question for more
+    subscriptions_write_subscription('node', 'type', 'page', -1, $this->user_a->uid);
+    $this->assertSubscriptions($params, $this->user_a->uid, 1, 'User A has a subscription');
+    $this->assertSubscriptions(array(), $this->user_a->uid, 1, 'User A has a subscription');
+   
+    // subscribe user B to parent node
+    subscriptions_write_subscription('node', 'type', 'page', -1, $this->user_b->uid);
+    // check subscription of uB to nP
+    $this->assertSubscriptions($params, $this->user_b->uid, 1, 'User B has a subscription');
+    $this->assertSubscriptions($params, $this->user_a->uid, 1, 'User A still has a subscription');
+    
+    // Unsubscribe user A from specific subscription.
+    subscriptions_delete($this->user_a->uid, 'node', 'type', 'page');
+    $this->assertSubscriptions(array(), $this->user_b->uid, 1, 'User B still has a subscription');
+    $this->assertSubscriptions(array(), $this->user_a->uid, 0);
+     
+    // Unsubscribe user B from all subscriptions.
+    subscriptions_delete($this->user_b->uid);
+    $this->assertSubscriptions(array(), $this->user_b->uid, 0);
+  }
+  
+  /**
+   * Tests that correct notifications are being sent to users 
+   * subscribed to new content via subscriptions_reference
+   * @see subscriptions_reference_test.module in tests
+   */
+  function testUserGetsNotification() {
+    // @todo write tests that would check that correct emails are sent
+    // by emails cron
+  }
+
+}
+
+class SubscriptionContentUnitTestCase extends DrupalUnitTestCase {
+  
+  public static function getInfo() {
+    return array(
+      'name' => 'Subscriptions Content Unit Test Case',
+      'description' => 'Unit tests for subscription reference.',
+      'group' => 'Subscriptions',
+    );
+  }
+  
+  function test_Proper_Unit_Testing_with_Mocks() {
+    //  @todo find a way to mock the function _available_reference_fields() 
+    //  override_function(); this is one possible solution
+    //  http://php.net/manual/en/function.override-function.php
+    //  http://www.php.net/manual/en/function.override-function.php#50821
+    //  that requires: pecl apd
+    
+    //  overriding of the function will allow testing of other functions
+    //  without touching db
+    
+    //  here is an example of mock testing:
+    //  http://www.workhabit.com/blog/mock-function-testing-drupal
+    //  that requires: pecl runkit
+    //  @see PHPMockFunction folder in libraries
+    
+  }
+  /**
+   * Sumple test case to show how unit tests can be used.
+   */
+  function testAPIFunctionsExists() {
+   
+    $this->assertTrue(function_exists('subscriptions_write_subscription'), 'Function subscriptions_write_subscription() exists.');
+    
+  }
+  
+}
-- 
1.8.1.3


From c34218d938b99910db6f5c503b48fb50ecf3eec4 Mon Sep 17 00:00:00 2001
From: Bulat <byapparov@gmail.com>
Date: Sun, 12 May 2013 10:44:17 +0100
Subject: [PATCH 2/2] Added include clause for subscriptions.module file.

---
 subscriptions_content.test | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/subscriptions_content.test b/subscriptions_content.test
index 5c33e24..64c6943 100644
--- a/subscriptions_content.test
+++ b/subscriptions_content.test
@@ -122,7 +122,7 @@ class SubscriptionContentUnitTestCase extends DrupalUnitTestCase {
    * Sumple test case to show how unit tests can be used.
    */
   function testAPIFunctionsExists() {
-   
+    include_once('subscriptions.module');
     $this->assertTrue(function_exists('subscriptions_write_subscription'), 'Function subscriptions_write_subscription() exists.');
     
   }
-- 
1.8.1.3

