You are here

function Notifications_API_Tests::testNotificationsBasicAPI in Notifications 5

Play with creating, retrieving, deleting a pair subscriptions

File

tests/notifications_api.test, line 15

Class

Notifications_API_Tests
Class for testing notifications module. Tests basic API functions

Code

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

  // Login with a user who has Notifications admin rights

  //$user = $this->drupalCreateUserRolePerm(array('administer notifications'));

  //$this->drupalLoginUser($user);
  $user = user_load(array(
    'uid' => 1,
  ));
  $s1 = new Stdclass();
  $s1->uid = $user->uid;
  $s1->type = $test_type;
  $s1->event_type = $test_event_type;
  $s1->fields = array(
    'field1' => 1,
    'field2' => 2,
  );
  $s2 = $s1;

  // Create the subscription and check assigned sid
  notifications_save_subscription($s1);
  $this
    ->assertEqual(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
  notifications_save_subscription($s2);
  $this
    ->assertEqual($s1->sid, $s2->sid, 'A duplicate subscription has been detected and updated');

  // Now really create a second one
  $s2 = clone $s1;
  $s2->sid = 0;
  $s2->fields['field2'] = 3;
  notifications_save_subscription($s2);
  $this
    ->assertEqual(is_numeric($s2->sid) && $s2->sid > $s1->sid, TRUE, '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');

  // Test query builder, first basic query, then add some fields
  $query = notifications_query_build(array(
    'select' => 'SELECT field1',
    'join' => 'JOIN table1',
  ));
  $target = array(
    'select' => array(
      'SELECT field1',
    ),
    'join' => array(
      'JOIN table1',
    ),
    'where' => array(),
    'args' => array(),
  );
  $this
    ->assertEqual($query, $target, 'Build basic query with SELECT and JOIN.');
  $fields = array(
    'f1' => 1,
    'f2' => 'value2',
  );
  $query = notifications_query_build(array(
    'fields' => $fields,
  ), $query);
  $target['where'] = array(
    "f.field = '%s' AND f.value = '%s'",
    "f.field = '%s' AND f.value = '%s'",
  );
  $target['args'] = array(
    'f1',
    1,
    'f2',
    'value2',
  );
  $this
    ->assertEqual($query, $target, 'Build basic query with simple fields.');
  $fields = array(
    'f3' => array(
      1,
      2,
    ),
    'f4' => array(
      'value3',
      'value4',
    ),
  );
  $query = notifications_query_build(array(
    'fields' => $fields,
  ), $query);
  $target['where'][] = "f.field = '%s' AND f.value IN ('%s', '%s')";
  $target['where'][] = "f.field = '%s' AND f.value IN ('%s', '%s')";
  $target['args'] = array(
    'f1',
    1,
    'f2',
    'value2',
    'f3',
    1,
    2,
    'f4',
    'value3',
    'value4',
  );
  $this
    ->assertEqual($query, $target, 'Build basic query with array fields.');
}