You are here

function FlagFlaggingCRUDTestCase::testFlaggingCreate in Flag 7.3

Test creation of a flagging entity with flagging_save().

File

tests/flag.test, line 95
Tests for the Flag module.

Class

FlagFlaggingCRUDTestCase
Test CRUD operations on Flagging entities.

Code

function testFlaggingCreate() {

  // Create an article node that we try to create a flagging entity for.
  $title = $this
    ->randomName(8);
  $node = array(
    'title' => $title,
    'body' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => $this
            ->randomName(32),
        ),
      ),
    ),
    'uid' => 1,
    'type' => 'article',
    'is_new' => TRUE,
  );
  $node = node_submit((object) $node);
  node_save($node);

  // Create a flagging entity and save it.
  $flagging = array(
    'fid' => $this->flag->fid,
    'entity_type' => 'node',
    'entity_id' => $node->nid,
    'uid' => $this->flag_unflag_user->uid,
  );
  $flagging = (object) $flagging;
  flagging_save($flagging);

  // Test flagging has a flagging_id
  $this
    ->assertTrue(!empty($flagging->flagging_id), 'The flagging entity has an entity id.');

  // Test the database record exists.
  $result = db_query("SELECT * FROM {flagging} WHERE fid = :fid AND entity_id = :nid AND uid = :uid", array(
    ':fid' => $this->flag->fid,
    ':nid' => $node->nid,
    ':uid' => $this->flag_unflag_user->uid,
  ));
  $records = $result
    ->fetchAll();
  $this
    ->assertTrue(count($records), 'The flagging record exists in the database.');

  // Test node is flagged.
  // The current user is not the same as the user logged into the internal
  // browser, so we have to pass the UID param explicitly.
  $this
    ->assertTrue($this->flag
    ->is_flagged($node->nid, $this->flag_unflag_user->uid), 'The node has been flagged by creating the flagging.');
}