You are here

class Notifications_API_Tests in Notifications 5

Class for testing notifications module. Tests basic API functions

Hierarchy

Expanded class hierarchy of Notifications_API_Tests

File

tests/notifications_api.test, line 6

View source
class Notifications_API_Tests extends DrupalTestCase {
  function get_info() {
    return array(
      'name' => 'Notifications API',
      'group' => 'Notifications',
      'desc' => 'Notifications API functions',
    );
  }

  /**
   * Play with creating, retrieving, deleting a pair subscriptions
   */
  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.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalTestCase::$_cleanupModules property
DrupalTestCase::$_cleanupRoles property
DrupalTestCase::$_cleanupUsers property
DrupalTestCase::$_cleanupVariables property
DrupalTestCase::$_content property
DrupalTestCase::assertCopy function Will trigger a pass if both parameters refer to different objects. Fail otherwise.
DrupalTestCase::assertEqual function Will trigger a pass if the two parameters have the same value only. Otherwise a fail.
DrupalTestCase::assertError function Confirms that an error has occurred and optionally that the error text matches exactly.
DrupalTestCase::assertErrorPattern function Confirms that an error has occurred and that the error text matches a Perl regular expression.
DrupalTestCase::assertIdentical function Will trigger a pass if the two parameters have the same value and same type. Otherwise a fail.
DrupalTestCase::assertIsA function Type and class test. Will pass if class matches the type name or is a subclass or if not an object, but the type is correct.
DrupalTestCase::assertNoErrors function Confirms that no errors have occurred so far in the test method.
DrupalTestCase::assertNotA function Type and class mismatch test. Will pass if class name or underling type does not match the one specified.
DrupalTestCase::assertNotEqual function Will trigger a pass if the two parameters have a different value. Otherwise a fail.
DrupalTestCase::assertNotIdentical function Will trigger a pass if the two parameters have the different value or different type.
DrupalTestCase::assertNotNull function Will be true if the value is set.
DrupalTestCase::assertNoUnwantedPattern function Will trigger a pass if the Perl regex pattern is not present in subject. Fail if found.
DrupalTestCase::assertNoUnwantedRaw function Will trigger a pass if the raw text is NOT found on the loaded page Fail otherwise.
DrupalTestCase::assertNull function Will be true if the value is null.
DrupalTestCase::assertReference function Will trigger a pass if both parameters refer to the same object. Fail otherwise.
DrupalTestCase::assertWantedPattern function Will trigger a pass if the Perl regex pattern is found in the subject. Fail otherwise.
DrupalTestCase::assertWantedRaw function Will trigger a pass if the raw text is found on the loaded page Fail otherwise.
DrupalTestCase::clickLink function Follows a link by name. Will click the first link found with this link text by default, or a later one if an index is given. Match is case insensitive with normalised space. Does make assertations if the click was sucessful or not and it does…
DrupalTestCase::drupalCheckAuth function @abstract Checks to see if we need to send a http-auth header to authenticate when browsing a site.
DrupalTestCase::drupalCreateRolePerm function Create a role / perm combination specified by permissions
DrupalTestCase::drupalCreateUserRolePerm function Creates a user / role / permissions combination specified by permissions
DrupalTestCase::drupalGet function @abstract Brokder for the get function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::drupalGetContent function @TODO: needs documentation
DrupalTestCase::drupalLoginUser function Logs in a user with the internal browser
DrupalTestCase::drupalModuleDisable function Disables a drupal module
DrupalTestCase::drupalModuleEnable function Enables a drupal module
DrupalTestCase::drupalPostRequest function Do a post request on a drupal page. It will be done as usual post request with SimpleBrowser
DrupalTestCase::drupalRawPost function @abstract Broker for the post function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::DrupalTestCase function
DrupalTestCase::drupalVariableSet function Set a druapl variable and keep track of the changes for tearDown()
DrupalTestCase::randomName function Generates a random string, to be used as name or whatever
DrupalTestCase::run function Just some info for the reporter
DrupalTestCase::tearDown function tearDown implementation, setting back switched modules etc 1
Notifications_API_Tests::get_info function
Notifications_API_Tests::testNotificationsBasicAPI function Play with creating, retrieving, deleting a pair subscriptions