You are here

public function SlickTestCase::testOptionSetCrud in Slick Carousel 7.2

Tests Slick optionset CRUD.

File

tests/slick.test, line 87
Provides test cases for Slick carousel based on flexslider.test.

Class

SlickTestCase
Tests the Slick optionsets, configuration options and permission controls.

Code

public function testOptionSetCrud() {
  module_load_include('inc', 'slick', 'includes/slick.admin');

  // Login as the admin user.
  $this
    ->drupalLogin($this->adminUser);
  $testsets = array(
    'testset1',
    'testset2',
  );
  foreach ($testsets as $name) {

    // Create a new optionset with default settings.
    $optionset = slick_optionset_create(array(
      'name' => $name,
    ));
    $this
      ->assertTrue($optionset->name == $name, t('Optionset object created: <strong>@name</strong>', array(
      '@name' => $optionset->name,
    )));
    $this
      ->assertFalse(empty($optionset->options), 'Create optionset works.');

    // Save the optionset to the database.
    $optionset = slick_optionset_save($optionset, TRUE);
    $this
      ->assertFalse(FALSE === $optionset, 'Optionset saved to database.');

    // Read the values from the database.
    $optionset = slick_optionset_load($name);
    $this
      ->assertTrue(is_object($optionset), t('Loaded optionset: <strong>@name</strong>', array(
      '@name' => $optionset->name,
    )));
    $this
      ->assertEqual($name, $optionset->name, t('Loaded name matches: <strong>@name</strong>', array(
      '@name' => $optionset->name,
    )));

    // Ensure defaults match the custom saved data when no overrides.
    $default = slick_optionset_create();
    foreach ((array) $default->options['settings'] as $key => $value) {
      $new_value = $optionset->options['settings'][$key];
      $read_value = $this
        ->getPrintedValue($value);
      $read_new_value = $this
        ->getPrintedValue($new_value);
      $message = t('<strong>@key</strong>: default:<strong>@value</strong> matches saved:<strong>@new_value</strong>.', array(
        '@key' => $key,
        '@value' => $read_value,
        '@new_value' => $read_new_value,
      ));
      $this
        ->assertEqual($value, $new_value, $message);
    }
  }

  // Load all optionsets with a reset.
  $optionsets = slick_optionset_load_all(TRUE);
  $this
    ->assertTrue(is_array($optionsets), 'Available optionsets loaded');
  $message = t('Proper number of optionsets loaded (two created, one default): <strong>@count</strong>', array(
    '@count' => count($optionsets),
  ));
  $this
    ->assertTrue(count($optionsets) == 3, $message);

  // Ensure they all loaded correctly.
  foreach ($optionsets as $key => $optionset) {
    $this
      ->assertTrue(isset($optionset->name), t('<strong>@key</strong>: Loaded optionsets have a defined machine <strong>@name</strong>', array(
      '@key' => $key,
      '@name' => $optionset->name,
    )));
    $this
      ->assertTrue(isset($optionset->label), t('<strong>@key</strong>: Loaded optionsets have a defined human readable name <strong>@label</strong>', array(
      '@key' => $key,
      '@label' => $optionset->label,
    )));
    $this
      ->assertTrue(isset($optionset->options), t('<strong>@key</strong>: Loaded optionsets have a defined array of options', array(
      '@key' => $key,
    )));
  }

  // Update the optionset.
  $test_options = $this
    ->getOptions();
  $test_options = $test_options['valid'];

  // Load one of the test optionset.
  $test = $testsets[1];
  $optionset = slick_optionset_load($test);

  // Compare saved options different from the set2 options.
  foreach ($test_options['set2'] as $key => $value) {
    $saved_value = $optionset->options['settings'][$key];
    $read_value = $this
      ->getPrintedValue($value);
    $read_saved_value = $this
      ->getPrintedValue($saved_value);
    $message = t('<strong>@key</strong>: saved value:<strong>@saved_value</strong> can be overriden by set2:<strong>@value</strong>.', array(
      '@key' => $key,
      '@saved_value' => $read_saved_value,
      '@value' => $read_value,
    ));
    $this
      ->assertNotEqual($saved_value, $value, $message);
  }

  // Union the saved values to use the overrides now.
  $optionset->options['settings'] = (array) $test_options['set2'] + (array) $optionset->options['settings'];

  // Save the updated values.
  $optionset = slick_optionset_save($optionset);
  $this
    ->assertFalse(FALSE == $optionset, 'Saved updates to optionset to database.');

  // Load the values from the database again.
  $optionset = slick_optionset_load($test);

  // Compare saved options match the set2 options.
  foreach ($test_options['set2'] as $key => $value) {
    $saved_value = $optionset->options['settings'][$key];
    $read_value = $this
      ->getPrintedValue($value);
    $read_saved_value = $this
      ->getPrintedValue($saved_value);
    $message = t('<strong>@key</strong>: saved value:<strong>@saved_value</strong> matches set2:<strong>@value</strong>.', array(
      '@key' => $key,
      '@saved_value' => $read_saved_value,
      '@value' => $read_value,
    ));
    $this
      ->assertEqual($saved_value, $value, $message);
  }

  // Delete the optionset.
  $this
    ->assertTrue(slick_optionset_exists($test), t('Optionset <strong>@name</strong> exists and is ready to be deleted and reverted.', array(
    '@name' => $test,
  )));

  // Remove from db, but kept in code with slick_optionset_create() above.
  slick_optionset_delete($optionset);

  // Ensure the optionset is reverted to code after deletion from DB.
  $this
    ->assertTrue(slick_optionset_exists($test), t('Optionset <strong>@name</strong> is deleted from DB, and reverted to code.', array(
    '@name' => $test,
  )));
}