diff --git a/tests/flag.test b/tests/flag.test index 7397890..4d74f5b 100644 --- a/tests/flag.test +++ b/tests/flag.test @@ -213,6 +213,308 @@ class FlagFlaggingCRUDTestCase extends FlagTestCaseBase { } /** + * Test effects of CRUD operations on flagged entities on Flagging entities. + */ +class FlagEntityFlaggingCRUDTestCase extends FlagTestCaseBase { + + /** + * Implements getInfo(). + */ + public static function getInfo() { + return array( + 'name' => t('Flagging Entity CRUD'), + 'description' => t('Test effects of CRUD operations on flagged entities on Flagging entities.'), + 'group' => t('Flag'), + ); + } + + /** + * Implements setUp(). + */ + function setUp() { + // We need text module to make a simple field to test. + parent::setUp('flag', 'text'); + + $flag_data = array( + 'entity_type' => 'node', + 'name' => 'test_flag_node', + 'title' => 'Test Flag node', + 'global' => 0, + 'types' => array( + 0 => 'article', + ), + 'flag_short' => 'Flag this item', + 'flag_long' => '', + 'flag_message' => '', + 'unflag_short' => 'Unflag this item', + 'unflag_long' => '', + 'unflag_message' => '', + 'unflag_denied_text' => 'You may not unflag this item', + 'flag_confirmation' => 'Are you sure you want to flag this item?', + 'unflag_confirmation' => 'Are you sure you want to remove this flag?', + 'link_type' => 'confirm', + 'weight' => 0, + 'show_on_form' => 0, + 'access_author' => '', + 'show_contextual_link' => 0, + 'show_in_links' => array( + 'full' => 1, + 'teaser' => 1, + ), + 'i18n' => 0, + 'api_version' => 3, + ); + $this->node_flag = $this->createFlag($flag_data); + + $flag_data = array( + 'entity_type' => 'user', + 'name' => 'test_flag_user', + 'title' => 'Test Flag user', + 'global' => 0, + 'types' => array(), + 'flag_short' => 'Flag this user', + 'flag_long' => '', + 'flag_message' => '', + 'unflag_short' => 'Unflag this user', + 'unflag_long' => '', + 'unflag_message' => '', + 'unflag_denied_text' => 'You may not unflag this user', + 'flag_confirmation' => 'Are you sure you want to flag this user?', + 'unflag_confirmation' => 'Are you sure you want to remove this flag?', + 'link_type' => 'confirm', + 'weight' => 0, + 'show_on_form' => 0, + 'access_author' => '', + 'show_contextual_link' => 0, + 'show_in_links' => array( + 'full' => 1, + 'teaser' => 1, + ), + 'i18n' => 0, + 'api_version' => 3, + ); + $this->user_flag = $this->createFlag($flag_data); + + // Create a user who can flag and unflag users. + $this->flag_unflag_users_user = $this->drupalCreateUser(array( + // Needs to see profiles to see the flag link. + 'access user profiles', + // Will be deleting a user. + 'administer users', + 'flag test_flag_user', + 'unflag test_flag_user', + )); + + // Create test user who can flag and unflag nodes. + $this->flag_unflag_user = $this->drupalCreateUser(array( + 'flag test_flag_node', + 'unflag test_flag_node', + )); + $this->drupalLogin($this->flag_unflag_user); + + // Create a text field on the flaggings. + $field = array( + 'field_name' => 'test_flagging_field', + 'type' => 'text', + ); + field_create_field($field); + + $instance = array( + 'field_name' => 'test_flagging_field', + 'entity_type' => 'flagging', + 'bundle' => 'test_flag_node', + ); + field_create_instance($instance); + + $instance = array( + 'field_name' => 'test_flagging_field', + 'entity_type' => 'flagging', + 'bundle' => 'test_flag_user', + ); + field_create_instance($instance); + } + + /** + * Test creation and deletion of a Flagging entity for a node. + */ + function testNodeFlagging() { + // 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); + $nid = $node->nid; + + $this->drupalGet("node/$nid"); + + $this->assertText('Flag this item', 'The flag link appears on the node'); + + // Get the flagging confirm form. + $this->drupalGet("flag/confirm/flag/test_flag_node/$nid?destination=node/$nid"); + + // Flag the node, entering field data for the flagging. + $field_text_value = $this->randomString(); + $edit = array( + 'test_flagging_field[und][0][value]' => $field_text_value, + ); + $this->drupalPost("flag/confirm/flag/test_flag_node/$nid", $edit, t('Flag this item'), array( + 'query' => array( + 'destination' => "node/$nid", + ), + )); + + $this->assertText('Unflag this item', 'The unflag link appears on the node.'); + + // Reset the cache, then load the flagging. + entity_get_controller('flagging')->resetCache(); + + $result = db_query("SELECT flagging_id FROM {flagging} WHERE entity_type = 'node' AND entity_id = $nid"); + $flagging_id = $result->fetchField(); + + $flagging = flagging_load($flagging_id); + + $items = field_get_items('flagging', $flagging, 'test_flagging_field'); + $this->assertEqual($field_text_value, $items[0]['value'], 'Flagging has field data.'); + + // Now delete the node. + node_delete($nid); + + // Check the flagging and its field data are deleted. + entity_get_controller('node')->resetCache(); + entity_get_controller('flagging')->resetCache(); + + $result = db_query("SELECT flagging_id FROM {flagging} WHERE entity_type = 'node' AND entity_id = $nid"); + $new_flagging_id = $result->fetchField(); + $this->assertFalse($new_flagging_id, 'The flagging entity has been deleted.'); + + $result = db_query("SELECT entity_id FROM {field_data_test_flagging_field} WHERE entity_type = 'flagging' AND entity_id = $flagging_id"); + $new_entity_id = $result->fetchField(); + $this->assertFalse($new_entity_id, 'The field data of the flagging on the node has been deleted.'); + } + + /** + * Test deletion of a Flagging entities for and by a user. + * + * We need to test that when a user is deleted, Flagging entities and their + * data is deleted for: + * - Flaggings the user has made + * - Flaggings on the actual user + */ + function testUserFlagging() { + // 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); + $nid = $node->nid; + + $this->drupalGet("node/$nid"); + + $this->assertText('Flag this item', 'The flag link appears on the node'); + + // Get the flagging confirm form. + $this->drupalGet("flag/confirm/flag/test_flag_node/$nid?destination=node/$nid"); + + // Flag the node, entering field data for the flagging. + $field_text_value = $this->randomString(); + $edit = array( + 'test_flagging_field[und][0][value]' => $field_text_value, + ); + $this->drupalPost("flag/confirm/flag/test_flag_node/$nid", $edit, t('Flag this item'), array( + 'query' => array( + 'destination' => "node/$nid", + ), + )); + + $this->assertText('Unflag this item', 'The unflag link appears on the node.'); + + // Reset the cache, then load the flagging. + entity_get_controller('flagging')->resetCache(); + + $result = db_query("SELECT flagging_id FROM {flagging} WHERE entity_type = 'node' AND entity_id = $nid"); + $flagging_id_node = $result->fetchField(); + + $flagging = flagging_load($flagging_id_node); + + $items = field_get_items('flagging', $flagging, 'test_flagging_field'); + $this->assertEqual($field_text_value, $items[0]['value'], 'Flagging on node has field data.'); + + // Now flag the user who just flagged the node. + // Log in as the user who may flag users. + $this->drupalLogin($this->flag_unflag_users_user); + $uid = $this->flag_unflag_user->uid; + $this->drupalGet("user/$uid"); + + $this->assertText('Flag this user', 'The flag link appears on the user.'); + + // Flag the user, entering field data for the flagging. + $field_text_value = $this->randomString(); + $edit = array( + 'test_flagging_field[und][0][value]' => $field_text_value, + ); + $this->drupalPost("flag/confirm/flag/test_flag_user/$uid", $edit, t('Flag this user'), array( + 'query' => array( + 'destination' => "user/$uid", + ), + )); + + $this->assertText('Unflag this user', 'The unflag link appears on the user profile.'); + + // Check the flagging entity is created and has field values. + $result = db_query("SELECT flagging_id FROM {flagging} WHERE entity_type = 'user' AND entity_id = $uid"); + $flagging_id_user = $result->fetchField(); + + $flagging = flagging_load($flagging_id_user); + + $items = field_get_items('flagging', $flagging, 'test_flagging_field'); + $this->assertEqual($field_text_value, $items[0]['value'], 'Flagging on user has field data.'); + + // Now delete the flagged user. + $this->drupalGet("user/$uid/cancel"); + $edit = array( + 'user_cancel_method' => 'user_cancel_delete', + ); + $this->drupalPost("user/$uid/cancel", $edit, 'Cancel account'); + + // Reset caches, then check everything was deleted. + entity_get_controller('user')->resetCache(); + entity_get_controller('flagging')->resetCache(); + + $deleted_user = user_load($uid); + $this->assertFalse($deleted_user, 'The flagged user has been deleted.'); + + // Check both flaggings and their field data are deleted. + $flagging = flagging_load($flagging_id_user); + $this->assertFalse($flagging, 'The flagging on the user has been deleted.'); + + $result = db_query("SELECT entity_id FROM {field_data_test_flagging_field} WHERE entity_type = 'flagging' AND entity_id = $flagging_id_user"); + $new_entity_id = $result->fetchField(); + $this->assertFalse($new_entity_id, 'The field data of the flagging on the user has been deleted.'); + + + $flagging = flagging_load($flagging_id_node); + $this->assertFalse($flagging, 'The flagging on the node has been deleted.'); + + $result = db_query("SELECT entity_id FROM {field_data_test_flagging_field} WHERE entity_type = 'flagging' AND entity_id = $flagging_id_node"); + $new_entity_id = $result->fetchField(); + $this->assertFalse($new_entity_id, 'The field data of the flagging on the node has been deleted.'); + } + +} + +/** * Test Flag admin UI. */ class FlagAdminTestCase extends FlagTestCaseBase {