You are here

public function PartyHatTestCase::testPartyHatCrud in Party 7

Same name and namespace in other branches
  1. 8.2 modules/party_hat/tests/party_hat.test \PartyHatTestCase::testPartyHatCrud()

Test party hat crud functionality through the UI and programatically.

File

modules/party_hat/tests/party_hat.test, line 51
Tests for the Party Hat module.

Class

PartyHatTestCase
Party Hat CRUD and Data Set tests.

Code

public function testPartyHatCrud() {

  ///////////////////////////////////////////////////////////////////////////

  //                Party Hat CRUD User Interface Tests

  ///////////////////////////////////////////////////////////////////////////

  // Test Party Hat Creation.
  $random_string = $this
    ->randomName();
  $hat_label = 'Test Hat ' . $random_string;
  $hat_machine_name = 'test_hat_' . strtolower($random_string);
  $edit = array();
  $edit['label'] = $hat_label;
  $edit['name'] = $hat_machine_name;
  $edit['description'] = "A test hat.";
  $edit['data_set_rules[profile2_main][has]'] = TRUE;
  $this
    ->drupalPost('admin/community/hats/add', $edit, t('Save'));

  // Assert that the Party Hat form submitted successfully and the Party was
  // saved.
  $this
    ->assertRaw(t('Party hat has been saved'), 'Test hat created successfully');

  // Test Party Hat Reading.
  $this
    ->drupalGet('admin/community/hats');

  // Assert that the created hat is visible in the Party Hat list.
  $this
    ->assertRaw($hat_label, 'Hat name found.');

  // Make sure the hat is in the database.
  $hid = db_query("SELECT hid FROM {party_hat} WHERE name = :hat_machine_name", array(
    ':hat_machine_name' => $hat_machine_name,
  ))
    ->fetchField();
  $hat_str = 'Hat ' . $hat_label;
  $message = empty($hid) ? 'Hat ' . $hat_label . ' machine name not found in party_hat table' : 'Hat ' . $hat_label . ' machine name found in party_hat table';

  // Assert that the hat was found in the database.
  $this
    ->assertNotNull($hid, $message);

  // Test Party Hat Updating.
  $this
    ->drupalGet('admin/community/hats/manage/' . $hat_machine_name);

  // Assert that the 'Edit Hat' page was loaded successfully.
  $this
    ->assertRaw('Edit hat: ' . $hat_label, "Hat name for {$hat_str} found on Edit hat page.");
  $edit = array();
  $edit['label'] = $hat_label . ' MODIFIED';
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $this
    ->drupalGet('admin/community/hats');

  // Assert that the Hat was correctly updated.
  $this
    ->assertRaw($hat_label . ' MODIFIED', 'Modified hat name found.');

  // Test Party Hat Deletion.
  $this
    ->drupalPost('/admin/community/hats/manage/' . $hat_machine_name . '/delete', array(), 'Confirm');
  $this
    ->drupalGet('admin/community/hats');

  // Assert that the Party Hat can no longer be found on the Party Hat list.
  $this
    ->assertNoRaw($hat_label, 'Test Hat not found (delete worked).');

  ///////////////////////////////////////////////////////////////////////////

  //                    Party Hat CRUD Programmatic Tests

  ///////////////////////////////////////////////////////////////////////////

  // Test Programmatic Party Hat Creation
  $random_string = $this
    ->randomName();
  $hat_label = 'Prog Test Hat ' . $random_string;
  $hat_machine_name = 'prog_test_hat_' . strtolower($random_string);
  $values = array(
    'name' => $hat_machine_name,
    // machine name
    'label' => $hat_label,
    // human readable label of the hat
    'description' => 'A test hat.',
    'data' => array(
      'profile2_main' => array(
        //use main profile
        'has' => 1,
      ),
    ),
  );
  $hat = entity_create('party_hat', $values);

  // Assert that the Party Hat object was created successfully.
  $this
    ->assertNotNull($hat);
  $hat
    ->save();
  $this
    ->drupalGet('admin/community/hats');

  // Assert that created hat is visible on the Party Hats List
  $this
    ->assertRaw($hat_label, 'Prog Test Hat found.');

  // Test Party Hat Reading.
  $hid = $hat->hid;
  unset($hat);
  $hat = party_hat_load($hat_machine_name);

  // Assert a hat object was loaded.
  $this
    ->assertNotNull($hat);

  // Assert that the correct Party Hat has been loaded.
  $this
    ->assertEqual($hid, $hat->hid);

  // Make sure the hat is in the db
  $hid_db = db_query("SELECT hid FROM {party_hat} WHERE name = :hat_machine_name", array(
    ':hat_machine_name' => $hat_machine_name,
  ))
    ->fetchField();

  // Assert that the correct machine name was found in the partY_hat table.
  $this
    ->assertNotNull($hid_db, 'Hat ' . $hat_label . ' machine name found in party_hat table');

  // Assert that the loaded hat hid matches the hat saved earlier.
  $this
    ->assertEqual($hid, $hid_db, 'party_hat_load() hid matches db hid');

  // Test Party Hat Updating.
  // Update the Hat label.
  $hat->label = $hat->label . ' MODIFIED';
  $hat
    ->save();
  unset($hat);
  $hat = party_hat_load($hat_machine_name);

  // Assert that the hat label hast been correctly saved into the database.
  $this
    ->assertEqual($hat_label . ' MODIFIED', $hat->label);

  // Test Party Hat Deletion
  entity_delete('party_hat', $hat->hid);
  $this
    ->drupalGet('admin/community/hats');

  // Assert that the Party Hat is no longer visible on the hat list page.
  $this
    ->assertNoRaw($hat_label, 'Prog Test Hat not found (delete worked).');

  // Make sure the hat is not in the db
  $hid_db = db_query("SELECT hid FROM {party_hat} WHERE name = :hat_machine_name", array(
    ':hat_machine_name' => $hat_machine_name,
  ))
    ->fetchField();

  // Assert that the deleted hat could not be loaded from the database.
  $this
    ->assertFalse($hid_db, 'Prog Test Hat not found in party_hat table (delete worked) ');
}