You are here

function NotificationsBasicTests::testNotificationsBasicAPI in Notifications 6.3

Same name and namespace in other branches
  1. 6.4 tests/notifications_api.test \NotificationsBasicTests::testNotificationsBasicAPI()
  2. 6 tests/notifications_api.test \NotificationsBasicTests::testNotificationsBasicAPI()
  3. 6.2 tests/notifications_api.test \NotificationsBasicTests::testNotificationsBasicAPI()
  4. 7 tests/notifications_api.test \NotificationsBasicTests::testNotificationsBasicAPI()

Play with creating, retrieving, deleting a pair subscriptions

File

tests/notifications_api.test, line 23

Class

NotificationsBasicTests
Class for testing notifications module. Tests basic API functions

Code

function testNotificationsBasicAPI() {
  $test_type = 'test';
  $test_event_type = 'test event';

  // Any user will do for exercising the API
  $user = $this
    ->drupalCreateUser();
  $s1 = new Stdclass();
  $s1->uid = $user->uid;
  $s1->type = $test_type;
  $s1->event_type = $test_event_type;
  $s1->fields = array(
    'field1' => 1,
    'field2' => 2,
  );
  $s1->destination = '';
  $s2 = clone $s1;

  // Create the subscription and check assigned sid
  $result = notifications_save_subscription($s1);
  $this
    ->assertEqual($result == SAVED_NEW && is_numeric($s1->sid) && $s1->sid > 0, TRUE, 'The subscription has been created');

  // Retrieve the subscription and check values
  $s = notifications_load_subscription($s1->sid, TRUE);
  $this
    ->assertEqual($s, $s1, 'The subscription has been retrieved and values are ok');

  // Attempt to create a second one with the same values
  $result = notifications_save_subscription($s2);
  $this
    ->assertTrue($result == SAVED_UPDATED && $s1->sid == $s2->sid, 'A duplicate subscription has been detected and updated');

  // Now change some field, try again with a different send method
  unset($s2->sid);
  $s2->send_method = 'test';
  $result = notifications_save_subscription($s2);
  $this
    ->assertTrue($result == SAVED_NEW && is_numeric($s2->sid) && $s2->sid > $s1->sid, 'Another subscription has been created');

  // Now change values for the second one
  unset($s2->send_method);
  $s2->fields['field2'] = 3;
  $result = notifications_save_subscription($s2);
  $this
    ->assertEqual($result, SAVED_UPDATED, 'Another subscription has been created');

  // Trying several recovery options
  $subs = notifications_get_subscriptions(array(
    'type' => $test_type,
  ));
  $this
    ->assertEqual(count($subs), 2, 'Retrieved subscriptions by type');
  $subs = notifications_get_subscriptions(array(
    'type' => $test_type,
  ), array(
    'field1' => $s1->fields['field1'],
  ), TRUE);
  $this
    ->assertEqual(count($subs), 0, 'Retrieved subscriptions by type and limited field');
  $subs = notifications_get_subscriptions(array(
    'type' => $test_type,
  ), array(
    'field1' => $s1->fields['field1'],
  ), FALSE);
  $this
    ->assertEqual(count($subs), 2, 'Retrieved subscriptions by type and general field');
  $subs = notifications_get_subscriptions(array(
    'type' => $test_type,
  ), array(
    'field1' => $s1->fields['field1'],
    'field2' => $s1->fields['field2'],
  ), FALSE);
  $this
    ->assertEqual(count($subs), 1, 'Retrieved subscriptions by type and general field');

  // Delete the subscriptions and check
  notifications_delete_subscriptions(array(
    'type' => $test_type,
  ));
  $subs = notifications_get_subscriptions(array(
    'type' => $test_type,
  ));
  $this
    ->assertEqual(count($subs), 0, 'The subscriptions have been deleted');

  // Try notifications_lite API
  notifications_lite_send($user->uid, 'Test Subject', 'Test body');
  $this
    ->assertEqual($this
    ->countQueued(array(
    'uid' => $user->uid,
  )), 1, 'Notification queued with notifications_lite');
}