123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- /**
- * @file
- * Test case for Testing the node API example module.
- *
- * This file contains the test cases to check if module is performing as
- * expected.
- */
- /**
- * Functional tests for the NodeAPI Example module.
- *
- * @ingroup nodeapi_example
- */
- class NodeApiExampleTestCase extends DrupalWebTestCase {
- /**
- * User object to perform site browsing
- * @var object
- */
- protected $webUser;
- /**
- * Content type to attach the rating system
- * @var string
- */
- protected $type;
- /**
- * {@inheritdoc}
- */
- public static function getInfo() {
- return array(
- 'name' => 'Node API example functionality',
- 'description' => 'Demonstrate Node API hooks that allow altering a node. These are the former hook_nodeapi.',
- 'group' => 'Examples',
- );
- }
- /**
- * Enables modules and create user with specific permissions.
- */
- public function setUp() {
- parent::setUp('nodeapi_example');
- // Create admin user. This module has no access control, so we can use a
- // trusted user. Revision access and revert permissions are required too.
- $this->webUser = $this->drupalCreateUser(array(
- // Required to set revision checkbox.
- 'administer nodes',
- 'administer content types',
- 'bypass node access',
- 'view revisions',
- 'revert revisions',
- ));
- // Login the admin user.
- $this->drupalLogin($this->webUser);
- }
- /**
- * Log user in, creates an example node, and uses the rating system.
- */
- public function testNodeExampleBasic() {
- // Login the user.
- $this->drupalLogin($this->webUser);
- // Create custom content type.
- $content_type = $this->drupalCreateContentType();
- $type = $content_type->type;
- // Go to edit the settings of this content type.
- $this->drupalGet('admin/structure/types/manage/' . $type);
- $this->assertResponse(200);
- // Check if the new Rating options appear in the settings page.
- $this->assertText(t('NodeAPI Example Rating'), 'Rating options found in content type.');
- $this->assertFieldByName('nodeapi_example_node_type', 1, 'Rating is Disabled by default.');
- // Disable the rating for this content type: 0 for Disabled, 1 for Enabled.
- $content_settings = array(
- 'nodeapi_example_node_type' => 0,
- );
- $this->drupalPost('admin/structure/types/manage/' . $type, $content_settings, t('Save content type'));
- $this->assertResponse(200);
- $this->assertRaw(' has been updated.', 'Settings modified successfully for content type.');
- // Create an example node.
- $langcode = LANGUAGE_NONE;
- $edit = array(
- "title" => $this->randomName(),
- );
- $this->drupalPost('node/add/' . $type, $edit, t('Save'));
- $this->assertResponse(200);
- // Check that the rating is not shown, as we have not yet enabled it.
- $this->assertNoRaw('Rating: <em>', 'Extended rating information is not shown.');
- // Save current current url (we are viewing the new node).
- $node_url = $this->getUrl();
- // Enable the rating for this content type: 0 for Disabled, 1 for Enabled.
- $content_settings = array(
- 'nodeapi_example_node_type' => TRUE,
- );
- $this->drupalPost('admin/structure/types/manage/' . $type, $content_settings, t('Save content type'));
- $this->assertResponse(200);
- $this->assertRaw(' has been updated.', 'Settings modified successfully for content type.');
- // Check previously create node. It should be not rated.
- $this->drupalGet($node_url);
- $this->assertResponse(200);
- $this->assertRaw(t('Rating: %rating', array('%rating' => t('Unrated'))), 'Content is not rated.');
- // Rate the content, 4 is for "Good"
- $rate = array(
- 'nodeapi_example_rating' => 4,
- );
- $this->drupalPost($node_url . '/edit', $rate, t('Save'));
- $this->assertResponse(200);
- // Check that content has been rated.
- $this->assertRaw(t('Rating: %rating', array('%rating' => t('Good'))), 'Content is successfully rated.');
- }
- /**
- * Test revisions of ratings.
- *
- * Logs user in, creates an example node, and tests rating functionality with
- * a node using revisions.
- */
- public function testNodeExampleRevision() {
- // Login the user.
- $this->drupalLogin($this->webUser);
- // Create custom content type.
- $content_type = $this->drupalCreateContentType();
- $type = $content_type->type;
- // Go to edit the settings of this content type.
- $this->drupalGet('admin/structure/types/manage/' . $type);
- $this->assertResponse(200);
- // Check if the new Rating options appear in the settings page.
- $this->assertText(t('NodeAPI Example Rating'), 'Rating options found in content type.');
- $this->assertFieldByName('nodeapi_example_node_type', 1, 'Rating is Disabled by default.');
- // Disable the rating for this content type: 0 for Disabled, 1 for Enabled.
- $content_settings = array(
- 'nodeapi_example_node_type' => 0,
- );
- $this->drupalPost('admin/structure/types/manage/' . $type, $content_settings, t('Save content type'));
- $this->assertResponse(200);
- $this->assertRaw(' has been updated.', 'Settings modified successfully for content type.');
- // Create an example node.
- $langcode = LANGUAGE_NONE;
- $edit = array(
- "title" => $this->randomName(),
- );
- $this->drupalPost('node/add/' . $type, $edit, t('Save'));
- $this->assertResponse(200);
- // Check that the rating is not shown, as we have not yet enabled it.
- $this->assertNoRaw('Rating: <em>', 'Extended rating information is not shown.');
- // Save current current url (we are viewing the new node).
- $node_url = $this->getUrl();
- // Enable the rating for this content type: 0 for Disabled, 1 for Enabled.
- $content_settings = array(
- 'nodeapi_example_node_type' => TRUE,
- );
- $this->drupalPost('admin/structure/types/manage/' . $type, $content_settings, t('Save content type'));
- $this->assertResponse(200);
- $this->assertRaw(' has been updated.', 'Settings modified successfully for content type.');
- // Check previously create node. It should be not rated.
- $this->drupalGet($node_url);
- $this->assertResponse(200);
- $this->assertRaw(t('Rating: %rating', array('%rating' => t('Unrated'))), 'Content is not rated.');
- // Rate the content, 4 is for "Good"
- $rate = array(
- 'nodeapi_example_rating' => 4,
- );
- $this->drupalPost($node_url . '/edit', $rate, t('Save'));
- $this->assertResponse(200);
- // Check that content has been rated.
- $this->assertRaw(t('Rating: %rating', array('%rating' => t('Good'))), 'Content is successfully rated.');
- // Rate the content to poor using a new revision, 1 is for "Poor"
- $rate = array(
- 'nodeapi_example_rating' => 1,
- 'revision' => 1,
- );
- $this->drupalPost($node_url . '/edit', $rate, t('Save'));
- $this->assertResponse(200);
- // Check that content has been rated.
- $this->assertRaw(t('Rating: %rating', array('%rating' => t('Poor'))), 'Content is successfully rated.');
- // Now switch back to previous revision of the node.
- $this->drupalGet($node_url . '/revisions');
- // There is only a revision, so it must work just clicking the first link..
- $this->clickLink('revert');
- $revert_form = $this->getUrl();
- $this->drupalPost($revert_form, array(), t('Revert'));
- // Go back to the node page.
- $this->drupalGet($node_url);
- $this->assertResponse(200);
- // Check that content has been rated.
- $this->assertRaw(t('Rating: %rating', array('%rating' => t('Good'))), 'Content rating matches reverted revision.');
- }
- }
|