You are here

class Notifications_Content_Tests in Notifications 5

Class for testing notifications module. Tests basic API functions

Notes:

  • An exception (PHP warning) is thrown when content module (cck) is enabled, nothing to worry about.

Hierarchy

Expanded class hierarchy of Notifications_Content_Tests

File

tests/notifications_content.test, line 9

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

  /**
   * Play with creating, retrieving, deleting a pair subscriptions
   */
  function testNotificationsContent() {

    // Enable messaging debug mode so messages are not actually sent and create user for testing
    // Enable debug mode so messages are not actually sent and create user for testing
    $this
      ->drupalModuleEnable('messaging_debug');
    $this
      ->drupalVariableSet('messaging_debug', 0);
    $this
      ->drupalModuleEnable('notifications_content');
    $this
      ->drupalVariableSet('notifications_default_send_interval', 0);

    // Create a new content-type for creating the feed node
    $info = new Stdclass();
    $info->type = $this
      ->randomName();
    $info->name = 'Notifications ' . str_replace('_', ' ', $this
      ->randomName());
    $info->description = 'Test content type for notifications';
    $info->module = 'node';
    $info->has_title = TRUE;
    $info->title_label = t('Title');
    $info->has_body = TRUE;
    $info->body_label = t('Body');
    $info->min_word_count = 0;
    $info->custom = TRUE;
    node_type_save($info);

    // Author for testing, will be admin
    $author = user_load(array(
      'uid' => 1,
    ));

    // Fake node for testing
    $node = new Stdclass();
    $node->title = 'Notifications ' . $this
      ->randomName();
    $node->body = 'This is a test node for content subscriptions.';
    $node->type = $info->type;
    $node->uid = $author->uid;
    $node->status = 1;
    node_save($node);
    $subs_thread = new Stdclass();
    $subs_thread->type = 'thread';
    $subs_thread->event_type = 'node';
    $subs_thread->fields['nid'] = $node->nid;

    // Check generic info hooks with some random values
    $this
      ->assertEqual(notifications_subscription_types('thread', 'event_type'), 'node', 'Types hook retrieves right value.');
    $event_type = notifications_event_types('node', 'update');
    $this
      ->assertEqual($event_type['digest'], array(
      'node',
      'nid',
    ), 'Event types hook retrieves right value.');

    // Try permissions with anonymous user
    $user = drupal_anonymous_user();
    $this
      ->assertEqual(notifications_user_allowed('subscription', $user, $subs_thread), FALSE, 'Subscription not allowed for anonymous user');

    // Try permissions with a different user
    $user = $this
      ->drupalCreateUserRolePerm(array(
      'access content',
      'subscribe to content',
      'subscribe to content type',
      'subscribe to author',
    ));
    $this
      ->assertEqual(notifications_user_allowed('subscription', $user, $subs_thread), TRUE, 'Subscription is allowed for user with the right permissions');

    // Create a real thread subscription for a user
    $this
      ->drupalLoginUser($user);
    $link = notifications_get_link('subscribe', array(
      'uid' => $user->uid,
      'type' => 'thread',
      'fields' => array(
        'nid' => $node->nid,
      ),
      'confirm' => TRUE,
    ));
    $this
      ->drupalGet(url($link['href'], $link['query'], NULL, TRUE));
    $this
      ->assertText(t('Confirm your subscription'), 'Thread subscription: Subscriptions confirmation page is shown');
    $this
      ->assertWantedRaw(t('Thread: %title', array(
      '%title' => check_plain($node->title),
    )), 'Confirmation page parameters are ok');
    $this
      ->clickSubmit(t('Subscribe'));
    $this
      ->assertText(t('Your subscription was activated'), 'Confirmation message is displayed');

    // Create content type subscription, also check permissions
    $allowed = variable_get('notifications_content_types', array());
    $allowed[$info->type] = 0;
    $this
      ->drupalVariableSet('notifications_content_types', $allowed);
    $link = notifications_get_link('subscribe', array(
      'uid' => $user->uid,
      'type' => 'nodetype',
      'fields' => array(
        'type' => $node->type,
      ),
      'confirm' => TRUE,
    ));

    // First we should get an error message, content type not allowed
    $this
      ->drupalGet(url($link['href'], $link['query'], NULL, TRUE));
    $this
      ->assertText(t('Subscription type or parameters not allowed'), 'Error message for not allowed content type');

    // Check content type page before and after enabling this content type
    $this
      ->drupalGet(url('user/' . $user->uid . '/notifications/nodetype'));
    $this
      ->assertNoText($info->name, 'User account subscriptions doesn\'t show content type.');

    // Now we allow it and should get a confirmation page
    $allowed[$info->type] = 1;
    $this
      ->drupalVariableSet('notifications_content_types', $allowed);
    $this
      ->drupalGet(url($link['href'], $link['query'], NULL, TRUE));
    $this
      ->assertText(t('Confirm your subscription'), 'Content type: Subscriptions confirmation page is shown');
    $this
      ->assertWantedRaw(t('Content type: %type', array(
      '%type' => $info->name,
    )), 'Confirmation page parameters are ok');
    $this
      ->clickSubmit(t('Subscribe'));
    $this
      ->assertText(t('Your subscription was activated'), 'Confirmation message is displayed');

    // User account page should now display it
    $this
      ->drupalGet(url('user/' . $user->uid . '/notifications/nodetype'));
    $this
      ->assertText($info->name, 'User account subscriptions shows content type.');

    // Create author subscription for admin
    $link = notifications_get_link('subscribe', array(
      'uid' => $user->uid,
      'type' => 'author',
      'fields' => array(
        'author' => $author->uid,
      ),
      'confirm' => TRUE,
    ));
    $this
      ->drupalGet(url($link['href'], $link['query'], NULL, TRUE));
    $this
      ->assertText(t('Confirm your subscription'), 'Author: Subscriptions confirmation page is shown');
    $this
      ->assertWantedRaw(t('Author: %name', array(
      '%name' => $author->name,
    )), 'Confirmation page parameters are ok');
    $this
      ->clickSubmit(t('Subscribe'));
    $this
      ->assertText(t('Your subscription was activated'), 'Confirmation message is displayed');

    // Check subscriptions actually being created
    $subs = notifications_user_get_subscriptions($user->uid, 'node', $node->nid, $node, TRUE);
    $this
      ->assertEqual(count($subs), 3, 'The 3 subscriptions have actually been created');

    // Check user account pages
    $this
      ->drupalGet(url('user/' . $user->uid . '/notifications'));
    $this
      ->assertText(t('Thread'), 'User account overview shows threads.');
    $this
      ->assertText(t('Content type'), 'User account overview shows content type.');
    $this
      ->assertText(t('Author'), 'User account overview shows author.');
    $this
      ->drupalGet(url('user/' . $user->uid . '/notifications/thread'));
    $this
      ->assertText($node->title, 'User account subscriptions shows threads.');
    $this
      ->drupalGet(url('user/' . $user->uid . '/notifications/author'));
    $this
      ->assertText($author->name, 'User account subscriptions shows author.');

    // Make sure we have some queueing before going on and we are autosubscribed
    $this
      ->drupalVariableSet('notifications_send_immediate', 0);
    $this
      ->drupalVariableSet('notifications_sendself', 1);

    // Trigger a node update event
    $node = node_load($node->nid, NULL, TRUE);
    $node->body .= 'Updated.';
    node_save($node);

    // Check queued notifications. We should have three queued notifs at the end
    $send_method = notifications_user_setting('send_method', $user);
    $send_interval = notifications_user_setting('send_interval', $user);
    $count = db_result(db_query("SELECT count(*) FROM {notifications_queue} WHERE uid = %d AND send_method = '%s' AND send_interval = %d", $user->uid, $send_method, $send_interval));
    $this
      ->assertEqual($count, 3, 'We have the right number of rows in queue:' . $count);

    // Get messages from queue. After de-duping there should be only one.
    include_once drupal_get_path('module', 'notifications') . '/notifications.cron.inc';
    $queued = notifications_process_pull($send_method, array(
      $user->uid,
    ));
    $this
      ->assertEqual(count($queued), 1, 'Messages for this event have been queued.');

    // Simulate real queue processing and check queue has been cleaned.
    $max_sqid = notifications_process_prepare();
    $this
      ->assertEqual($max_sqid > 0, TRUE, 'Cleanup and queue prepare.');

    // Dirty trick for processing only these rows
    db_query("UPDATE {notifications_queue} SET module = 'notificationstesting' WHERE uid = %d", $user->uid);
    notifications_process_queue($send_interval, $max_sqid, 'notificationstesting');
    $count = db_result(db_query("SELECT count(*) FROM {notifications_queue} WHERE uid = %d", $user->uid));
    $this
      ->assertEqual($count, 0, 'All rows in queue have been processed.');

    // Remove created subscriptions
    notifications_delete_subscriptions(array(
      'uid' => $user->uid,
    ));

    // Remove temporary node and content-type
    node_delete($node->nid);
    node_type_delete($info->type);
  }

}

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_Content_Tests::get_info function
Notifications_Content_Tests::testNotificationsContent function Play with creating, retrieving, deleting a pair subscriptions