You are here

class Notifications_Templates_Tests in Notifications 5

Class for testing notifications module. Tests basic API functions

Hierarchy

Expanded class hierarchy of Notifications_Templates_Tests

File

tests/notifications_templates.test, line 6

View source
class Notifications_Templates_Tests extends DrupalTestCase {
  function get_info() {
    return array(
      'name' => 'Notifications Templates',
      'group' => 'Notifications',
      'desc' => 'Notifications templates and message composition',
    );
  }

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

    // Enable some modules and do not send out messages
    $this
      ->drupalModuleEnable('notifications_content');
    $this
      ->drupalModuleEnable('messaging_debug');
    $this
      ->drupalVariableSet('messaging_debug', 0);
    require_once drupal_get_path('module', 'notifications') . '/notifications.cron.inc';

    // Get site name to use in comparison
    $site_name = variable_get('site_name', 'Drupal');

    // Build some fake objects
    $node1 = (object) array(
      'nid' => 1,
      'uid' => 0,
      'type' => 'story',
      'title' => 'Title 1',
      'body' => 'Body 1',
    );
    $node2 = (object) array(
      'nid' => 2,
      'uid' => 1,
      'type' => 'page',
      'title' => 'Title 2',
      'body' => 'Body 2',
    );
    $event1 = (object) array(
      'eid' => 1,
      'type' => 'node',
      'action' => 'insert',
      'node' => $node1,
      'params' => array(
        'nid' => $node1->nid,
      ),
    );
    $event2 = (object) array(
      'eid' => 2,
      'type' => 'node',
      'action' => 'update',
      'node' => $node2,
      'params' => array(
        'nid' => $node2->nid,
      ),
    );

    // Do some fake event loading populating event objects
    $event1->objects['node'] = $node1;
    $event2->objects['node'] = $node2;

    // Basic api, text parts and info functions
    $this
      ->drupalVariableSet('notifications_digest_methods', array(
      1 => 'short',
      2 => 'long',
    ));
    $digest = notifications_digest_method(1, TRUE);
    $this
      ->assertEqual($digest['type'], 'short', 'Get information about intervals and digest methods.');
    $info = notifications_event_types($event1->type, $event1->action);
    $this
      ->assertEqual($info['digest'], array(
      'node',
      'type',
    ), 'Get event information about digest fields');
    $info = nofitications_digest_event_info($event1);
    $this
      ->assertEqual($info, array(
      'type' => 'node',
      'field' => 'type',
      'value' => 'story',
      'object' => $node1,
    ), 'Get digest information for first event.');

    //$this->printObject('digest information', $info);
    $info = nofitications_digest_event_info($event2);
    $this
      ->assertEqual($info, array(
      'type' => 'node',
      'field' => 'nid',
      'value' => 2,
      'object' => $node2,
    ), 'Get digest information for second event.');

    //$this->printObject('digest information', $info);

    // Text parts, text replacement, etc...
    $event1->text['test'] = 'Text part';
    $part = notifications_message_part('event', 'test', 'test', $event1);
    $this
      ->assertEqual($part, 'Text part', 'Retrieved message part from event');
    $part = notifications_message_part('type', 'key', 'test');
    $this
      ->assertEqual($part, 'type key [type-name] [title] [site-name]', 'Retrieved testing message part: ' . $part);
    $text = notifications_text_replace('[title] [type-name]', array(
      'node' => $node1,
    ));
    $this
      ->assertEqual($text, 'Title 1 Story', 'Text replacement for node object');

    // Now lets get into the scary part, events and event digesting
    $text = array(
      'subject' => 'Subject [title]',
      'header' => 'Update for [type-name] [title]',
      'main' => 'The body is [node-body-raw]',
      'footer' => 'My site is [site-name]',
      'digest' => 'Digest line [title]',
    );
    $target = array(
      'subject' => 'Subject Title 1',
      'body' => array(
        'header' => 'Update for Story Title 1',
        'event' => 'The body is Body 1',
        'footer' => 'My site is ' . $site_name,
      ),
    );
    $event1->text = $event2->text = $text;
    $message = notifications_process_message(NULL, $event1, array(), 'test');
    $this
      ->assertEqual($message, $target, 'Message composition for single event' . $this
      ->compareTexts($message, $target));

    // Test digesting, prepare events abn build event list
    $node3 = drupal_clone($node1);
    $node3->nid = 3;
    $node3->title = 'Title 3';

    // This should be digested by node type with the first one
    $event3 = drupal_clone($event1);
    $event3->eid = 3;
    $event3->objects['node'] = $node3;

    // This should be digested by node with the second one, it's the same node update
    $event4 = drupal_clone($event2);
    $event4->eid = 4;
    $event_list = array(
      1 => $event1,
      2 => $event2,
      3 => $event3,
      4 => $event4,
    );

    // This should produce a short digest, build the target to compare
    $test_line = '[type-name] [title] ' . $site_name;
    $body_common = array(
      'header' => 'digest header ' . $test_line,
      'footer' => 'digest footer ' . $test_line,
    );
    $lines = array();
    $lines['node']['story'] = array(
      'group' => array(
        'title' => 'digest title Story Title 1 ' . $site_name,
        'footer' => 'digest footer Story Title 1 ' . $site_name,
      ),
      'line' => array(
        1 => 'Digest line Title 1',
        2 => 'Digest line Title 3',
      ),
    );
    $lines['node'][2] = array(
      'group' => array(
        'title' => 'digest title Page Title 2 ' . $site_name,
        'footer' => 'digest footer Page Title 2 ' . $site_name,
      ),
      'line' => array(
        1 => 'Digest line Title 2',
        2 => 'Digest line Title 2',
      ),
    );
    $target = array(
      'subject' => 'digest subject ' . $test_line,
      'body' => theme('notifications_digest_short_body', $body_common, $lines),
    );
    $digest = notifications_process_send(NULL, $event_list, array(), 'test', 1);
    $message = array(
      'subject' => $digest[0]['subject'],
      'body' => $digest[0]['body'],
    );
    $this
      ->assertEqual($message, $target, 'Message composition for short digest.' . $this
      ->compareTexts($message, $target));

    //$this->printObject('Digest target', $target);

    //$this->printObject('Digest actual', $message);

    // This should be a long digest, interval 2, build target to compare
    $event_list = array(
      1 => $event1,
      2 => $event2,
    );
    $body = array(
      'The body is Body 1',
      'The body is Body 2',
    );
    $target = array(
      'subject' => 'digest subject [type-name] [title] ' . $site_name,
      'body' => theme('notifications_digest_long_body', $body_common['header'], $body, $body_common['footer']),
    );
    $digest = notifications_process_send(NULL, $event_list, array(), 'test', 2);
    $message = array(
      'subject' => $digest[0]['subject'],
      'body' => $digest[0]['body'],
    );
    $this
      ->assertEqual($message, $target, 'Message composition for long digest.' . $this
      ->compareTexts($message, $target));

    //$this->printObject('Digest target', $target['body']['content']);

    //$this->printObject('Digest actual', $message['body']['content']);
  }

  // Helper function to diff two text arrays
  function compareTexts($text1, $text2) {
    $diff = '';
    foreach ($text1 as $key => $value) {
      if (!isset($text2[$key])) {
        $diff .= "({$key})";
      }
      elseif (is_array($value)) {
        $diff .= $this
          ->compareTexts($text1[$key], $text2[$key]);
      }
      elseif ($value != $text2[$key]) {
        $diff .= "({$key}){$value}=" . $text2[$key];
      }
    }
    return $diff;
  }

  // Debug dump object
  function printObject($title, $object) {
    $this
      ->assertTrue(TRUE, $title . '= ' . print_r($object, TRUE));
  }

}

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_Templates_Tests::compareTexts function
Notifications_Templates_Tests::get_info function
Notifications_Templates_Tests::printObject function
Notifications_Templates_Tests::testNotificationsTemplateAPI function Play with creating, retrieving, deleting a pair subscriptions