You are here

public function ActivityUnitTest::testNodeActivityInsert in Activity 7

File

tests/activity_unit.test, line 157

Class

ActivityUnitTest

Code

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'));
}