You are here

class ActivityUnitTest in Activity 7

Hierarchy

Expanded class hierarchy of ActivityUnitTest

File

tests/activity_unit.test, line 143

View source
class ActivityUnitTest extends DrupalUnitTestCase {
  public static function getInfo() {
    return array(
      'name' => t('Activity Core Unit tests'),
      'description' => t('Unit tests for the Activity module'),
      'group' => t('Activity'),
    );
  }
  public function setUp() {
    parent::setUp('activity');
    require_once './' . drupal_get_path('module', 'activity') . '/activity_action_handlers.inc';
  }
  public function testNodeActivityInsert() {
    $handler = activity_load_handler('node_insert');
    $handler->templates = array(
      'en' => array(
        'node' => 'Node Author Message',
        'public' => 'Public Message',
      ),
    );
    $handler->options = array(
      'types' => array(),
      'view_modes' => array(),
    );
    $handler->actions_id = -1;
    $handler->label = 'Test node insert';
    $node1 = (object) array(
      'nid' => 1,
      'vid' => 1,
      'uid' => 0,
      // Avoids the window check, which queries the users table.
      'type' => 'article',
      'language' => 'und',
      'title' => 'Test1',
      'created' => REQUEST_TIME,
      'status' => 1,
    );
    $node2 = (object) array(
      'nid' => 2,
      'vid' => 2,
      'uid' => 0,
      'type' => 'page',
      'language' => 'und',
      'title' => 'Test2',
      'created' => REQUEST_TIME,
      'status' => 0,
    );
    $objects1 = array(
      'node' => $node1,
    );
    $objects2 = array(
      'node' => $node2,
    );

    // Make sure loaded objects match what is expected.
    $this
      ->assertEqual($node1->nid, $objects1['node']->nid, t('Loaded up the correct article node'));
    $this
      ->assertEqual($node2->nid, $objects2['node']->nid, t('Loaded up the correct page node'));

    // Mock around the valid method.
    $this
      ->assertTrue($handler
      ->valid($node1->nid, 1, REQUEST_TIME, $objects1, NULL, NULL), t('Activity handler correctly says article node is a valid activity'));
    $this
      ->assertTrue($handler
      ->valid($node2->nid, 1, REQUEST_TIME, $objects2, NULL, NULL), t('Activity handler correctly says page node is a valid activity'));
    $handler->options['types'] = array(
      'article' => 'article',
    );
    $this
      ->assertTrue($handler
      ->valid($node1->nid, 1, REQUEST_TIME, $objects1, NULL, NULL), t('Activity handler correctly checks article node type'));
    $this
      ->assertTrue(!$handler
      ->valid($node2->nid, 1, REQUEST_TIME, $objects2, NULL, NULL), t('Activity handler correctly checks page node type'));
    $handler->options['types'] = array(
      'page' => 'page',
    );
    $this
      ->assertTrue(!$handler
      ->valid($node1->nid, 1, REQUEST_TIME, $objects1, NULL, NULL), t('Activity handler prevents article from being valid'));
    $this
      ->assertTrue($handler
      ->valid($node2->nid, 1, REQUEST_TIME, $objects2, NULL, NULL), t('Activity handler properly validates page node'));

    // Ensure the proper eid is returned.
    $this
      ->assertEqual($node1->nid, $handler
      ->determineEid($objects1), t('Proper Eid is returned'));

    // Ensure the proper actor is returned.
    $this
      ->assertEqual($node1->uid, $handler
      ->determineActor($objects1), t('Proper Actor id is returned'));

    // Check timestamp.
    $this
      ->assertEqual($node2->created, $handler
      ->determineTimestamp($objects2), t('Returned proper timestamp'));

    // Check published flag.
    $this
      ->assertTrue($handler
      ->isPublished($objects1, $node1->uid), t('Correctly flagged activity as published'));
    $this
      ->assertTrue(!$handler
      ->isPublished($objects2, $node2->uid), t('Correctly flagged activity as not published'));
    $this
      ->assertEqual($handler
      ->determineNid($objects1), $objects1['node']->nid, t('Returned proper nid'));

    // Test tokenize method, which tests getUid.
    $objects1['node']->uid = 2;
    $messages = $handler
      ->tokenize($objects1);
    $this
      ->assertTrue(isset($messages['en']), t('Node insert returns english messages'));
    $this
      ->assertEqual($messages['en'][$objects1['node']->uid], $handler->templates['en']['node'], t('Node author message is returned'));
    $this
      ->assertEqual($messages['en'][0], $handler->templates['en']['public'], t('Returned proper public message'));
  }
  public function testCommentActivityInsert() {
    $handler = activity_load_handler('comment_insert');
    $handler->templates = array(
      'en' => array(
        'comment' => 'Comment Author Message',
        'node' => 'Node Author Message',
        'node_comment' => 'Node and Comment Message',
        'public' => 'Public Message',
      ),
    );
    $handler->options = array(
      'types' => array(),
      'view_modes' => array(),
    );
    $handler->actions_id = -1;
    $handler->label = 'Test comment insert';
    $node1 = (object) array(
      'nid' => 1,
      'vid' => 1,
      'uid' => 0,
      // Avoids the window check, which queries the users table.
      'type' => 'article',
      'language' => 'und',
      'title' => 'Test1',
      'created' => REQUEST_TIME,
      'status' => 1,
    );
    $node2 = (object) array(
      'nid' => 2,
      'vid' => 2,
      'uid' => 0,
      'type' => 'page',
      'language' => 'und',
      'title' => 'Test2',
      'created' => REQUEST_TIME,
      'status' => 0,
    );
    $comment1 = (object) array(
      'cid' => 1,
      'uid' => 0,
      'created' => REQUEST_TIME,
      'status' => 1,
    );
    $comment2 = (object) array(
      'cid' => 1,
      'uid' => 0,
      'created' => REQUEST_TIME,
      'status' => 0,
    );
    $objects1 = array(
      'node' => $node1,
      'comment' => $comment1,
    );
    $objects2 = array(
      'node' => $node2,
      'comment' => $comment2,
    );

    // Ensure the proper eid is returned.
    $this
      ->assertEqual($comment1->cid, $handler
      ->determineEid($objects1), t('Proper Eid is returned'));

    // Ensure the proper actor is returned.
    $this
      ->assertEqual($comment1->uid, $handler
      ->determineActor($objects1), t('Proper Actor id is returned'));

    // Check timestamp.
    $this
      ->assertEqual($comment1->created, $handler
      ->determineTimestamp($objects2), t('Returned proper timestamp'));

    // Check published flag.
    $this
      ->assertTrue($handler
      ->isPublished($objects1, $comment1->uid), t('Correctly flagged comment activity as published'));
    $this
      ->assertTrue(!$handler
      ->isPublished($objects2, $comment2->uid), t('Correctly flagged comment activity as not published'));
    $this
      ->assertEqual($handler
      ->determineNid($objects1), $objects1['node']->nid, t('Returned proper nid'));

    // Test to make sure getUid functions properly.
    $objects1['node']->uid = 2;
    $objects1['comment']->uid = 3;
    $messages = $handler
      ->tokenize($objects1);
    $this
      ->assertTrue(isset($messages['en']), t('English Messages are returned'));
    $this
      ->assertEqual($messages['en'][$objects1['comment']->uid], $handler->templates['en']['comment'], t('Comment author message is properly returned'));
    $this
      ->assertEqual($messages['en'][$objects1['node']->uid], $handler->templates['en']['node'], t('Node author message is properly returned'));
    $this
      ->assertEqual($messages['en'][$objects1['comment']->uid], $handler->templates['en']['comment'], t('Comment author message is properly returned'));
    $this
      ->assertEqual($messages['en'][0], $handler->templates['en']['public'], t('Properly return the public message'));

    // Check that the comment author == node author case.
    $objects1['comment']->uid = $objects1['node']->uid;
    $messages = $handler
      ->tokenize($objects1);
    $this
      ->assertEqual($messages['en'][$objects1['node']->uid], $handler->templates['en']['node_comment'], t('Comment and Node author message is properly handled'));
  }
  public function testUserActivityInsert() {
    $handler = activity_load_handler('user_insert');
    $handler->templates = array(
      'en' => array(
        'user' => 'Registered User',
        'public' => 'Public Message',
      ),
    );
    $handler->options = array(
      'roles' => array(),
    );
    $handler->actions_id = -1;
    $handler->label = 'Test user insert';
    $objects1['user'] = (object) array(
      'uid' => 7,
      'status' => 1,
      'created' => REQUEST_TIME - 68,
      'roles' => array(
        1 => 'admin',
      ),
    );
    $objects2['user'] = (object) array(
      'uid' => 45,
      'status' => 0,
      'created' => REQUEST_TIME - 678,
      'roles' => array(
        2 => 'other_role',
      ),
    );

    // Ensure the proper eid is returned.
    $this
      ->assertEqual($objects1['user']->uid, $handler
      ->determineEid(array(
      'account' => $objects1['user'],
    )), t('Proper Eid is returned'));

    // Ensure the proper actor is returned.
    $this
      ->assertEqual($objects1['user']->uid, $handler
      ->determineActor($objects1), t('Proper Actor id is returned'));

    // Check timestamp.
    $this
      ->assertEqual($objects1['user']->created, $handler
      ->determineTimestamp($objects1), t('Returned proper timestamp'));

    // Check published flag.
    $this
      ->assertTrue($handler
      ->isPublished($objects1, $objects1['user']->uid), t('Correctly flagged user insert activity as published'));
    $this
      ->assertTrue(!$handler
      ->isPublished($objects2, $objects2['user']->uid), t('Correctly flagged user insert activity as unpublished.'));

    // Check Role validation
    $handler->options = array(
      'roles' => array(
        1,
      ),
    );
    $this
      ->assertTrue($handler
      ->valid($objects1['user']->uid, $objects1['user']->uid, REQUEST_TIME, $objects1, NULL, NULL), t('Correctly marked a user registeration with role 1 as valid'));
    $this
      ->assertTrue(!$handler
      ->valid($objects2['user']->uid, $objects2['user']->uid, REQUEST_TIME, $objects2, NULL, NULL), t('Correctly marked a user registeration without role 1 as invalid'));
    $this
      ->assertEqual($handler
      ->determineNid($objects1), NULL, t('Returned proper a NULL nid'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActivityUnitTest::getInfo public static function
ActivityUnitTest::setUp public function Sets up unit test environment. Overrides DrupalUnitTestCase::setUp
ActivityUnitTest::testCommentActivityInsert public function
ActivityUnitTest::testNodeActivityInsert public function
ActivityUnitTest::testUserActivityInsert public function
DrupalTestCase::$assertions protected property Assertions thrown in that test case.
DrupalTestCase::$databasePrefix protected property The database prefix of this test run.
DrupalTestCase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
DrupalTestCase::$results public property Current results of this test case.
DrupalTestCase::$setup protected property Flag to indicate whether the test has been set up.
DrupalTestCase::$setupDatabasePrefix protected property
DrupalTestCase::$setupEnvironment protected property
DrupalTestCase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
DrupalTestCase::$testId protected property The test run ID.
DrupalTestCase::$timeLimit protected property Time limit for the test.
DrupalTestCase::$useSetupInstallationCache public property Whether to cache the installation part of the setUp() method.
DrupalTestCase::$useSetupModulesCache public property Whether to cache the modules installation part of the setUp() method.
DrupalTestCase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
DrupalTestCase::assert protected function Internal helper: stores the assert.
DrupalTestCase::assertEqual protected function Check to see if two values are equal.
DrupalTestCase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
DrupalTestCase::assertIdentical protected function Check to see if two values are identical.
DrupalTestCase::assertNotEqual protected function Check to see if two values are not equal.
DrupalTestCase::assertNotIdentical protected function Check to see if two values are not identical.
DrupalTestCase::assertNotNull protected function Check to see if a value is not NULL.
DrupalTestCase::assertNull protected function Check to see if a value is NULL.
DrupalTestCase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
DrupalTestCase::deleteAssert public static function Delete an assertion record by message ID.
DrupalTestCase::error protected function Fire an error assertion. 1
DrupalTestCase::errorHandler public function Handle errors during test runs. 1
DrupalTestCase::exceptionHandler protected function Handle exceptions.
DrupalTestCase::fail protected function Fire an assertion that is always negative.
DrupalTestCase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
DrupalTestCase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
DrupalTestCase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
DrupalTestCase::insertAssert public static function Store an assertion from outside the testing context.
DrupalTestCase::pass protected function Fire an assertion that is always positive.
DrupalTestCase::randomName public static function Generates a random string containing letters and numbers.
DrupalTestCase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
DrupalTestCase::run public function Run all tests in this class.
DrupalTestCase::verbose protected function Logs a verbose message in a text file.
DrupalUnitTestCase::tearDown protected function 1
DrupalUnitTestCase::__construct function Constructor for DrupalUnitTestCase. Overrides DrupalTestCase::__construct