You are here

public function SkinrApiTestCase::testSkinrSkinLoadSave in Skinr 7.2

Test skinr_skin_save() against invalid entries.

File

tests/skinr.test, line 523
Tests for the Skinr module.

Class

SkinrApiTestCase
Tests API functionality.

Code

public function testSkinrSkinLoadSave() {

  // Only save valid skins.
  $skin = (object) array(
    'theme' => '',
    'module' => 'block',
    'element' => 'system__user-menu',
    'skin' => 'skinr_test_subtheme',
    'options' => array(
      'option1',
      'option2',
    ),
    'status' => 1,
  );
  $this
    ->assertFalse(skinr_skin_save($skin), 'Skin configuration object not saved when $skin->theme is empty.');
  $skin->theme = 'skinr_test_subtheme';
  $skin->module = '';
  $this
    ->assertFalse(skinr_skin_save($skin), 'Skin configuration object not saved when $skin->module is empty.');
  $skin->module = 'block';
  $skin->element = '';
  $this
    ->assertFalse(skinr_skin_save($skin), 'Skin configuration object not saved when $skin->element is empty.');
  $skin->element = 'system-user-menu';
  $skin->skin = '';
  $this
    ->assertFalse(skinr_skin_save($skin), 'Skin configuration object not saved when $skin->skin is empty.');
  $skin->skin = 'skinr_test_subtheme';
  $skin->options = '';
  $this
    ->assertFalse(skinr_skin_save($skin), 'Skin configuration object not saved when $skin->options is not array.');
  $skin->options = array();
  $this
    ->assertFalse(skinr_skin_save($skin), 'Skin configuration object saved when $skin->options is empty array.');
  $skin->options = array(
    'option1' => 0,
    'option2' => 0,
  );
  $this
    ->assertFalse(skinr_skin_save($skin), 'Skin configuration object not saved when $skin->options is complex empty array.');
  $skin->options = array(
    'option1',
    'option2',
  );
  $this
    ->assertTrue(skinr_skin_save($skin), 'Skin configuration object was saved.');
  $this
    ->assertTrue(isset($skin->sid), 'SID added to skin configuration object.');
  $this
    ->assertTrue(isset($skin->uuid), 'UUID added to skin configuration object.');
  $this
    ->assertTrue(uuid_is_valid($skin->uuid), 'UUID for skin configuration object is valid.');

  // Test loading a skin configuration.
  $loaded_skin = skinr_skin_load($skin->sid);
  $this
    ->assertTrue(is_array($loaded_skin->options), 'Options for skin configuration object are unserialized.');
  $this
    ->assertTrue($loaded_skin->theme == $skin->theme && $loaded_skin->module == $skin->module && $loaded_skin->element == $skin->element && $loaded_skin->status == $skin->status && $loaded_skin->options[0] == $skin->options[0] && $loaded_skin->options[1] == $skin->options[1], 'Skin configuration object loaded.');

  // Test storage indicator.
  $this
    ->assertTrue(skinr_skin_storage($loaded_skin) == SKINR_STORAGE_IN_DATABASE, 'Storage indicator indicates stored in database.');

  // Save a second skin.
  module_load_include('inc', 'skinr', 'skinr.uuid');
  $second_skin = (object) array(
    'uuid' => uuid_generate(),
    'theme' => 'skinr_test_subtheme',
    'module' => 'block',
    'element' => 'system__main',
    'skin' => 'skinr_test_subtheme',
    'options' => array(
      'option3',
    ),
    'status' => 1,
  );
  skinr_skin_save($second_skin);

  // Test loading multiple skin configurations.
  $skins = skinr_skin_load_multiple(array(
    $skin->sid,
    $second_skin->sid,
  ));
  $this
    ->assertTrue(count($skins) == 2 && isset($skins[$skin->sid]->sid) && isset($skins[$second_skin->sid]->sid), 'Successfully loaded multiple skins.');

  // Test loading all skin configurations.
  drupal_static_reset('skinr_skin_load_multiple');
  $skins = skinr_skin_load_multiple(FALSE);
  $this
    ->assertTrue(count($skins) == 2 && isset($skins[$skin->sid]->sid) && isset($skins[$second_skin->sid]->sid), 'Successfully loaded all skins.');

  // Test $skin->uuid not overwritten when given.
  $this
    ->assertTrue($skins[$second_skin->sid]->uuid == $second_skin->uuid, 'UUID for skin configuration not overwritten when manually set.');

  // Test skinr_skin_uuid_to_sid().
  $this
    ->assertTrue(skinr_skin_uuid_to_sid($second_skin->uuid) == $second_skin->sid, 'Successfully got SID based on UUID for skin configuration object.');

  // Test skinr_skin_sid_to_uuid().
  $this
    ->assertTrue(skinr_skin_sid_to_uuid($second_skin->sid) == $second_skin->uuid, 'Successfully got UUID based on SID for skin configuration object.');

  // Test skinr_skin_load_by_uuid().
  $loaded_skin = skinr_skin_load_by_uuid($second_skin->uuid);
  $this
    ->assertTrue($loaded_skin->sid == $second_skin->sid, 'Skin configuration object loaded using UUID.');

  // Test skinr_skin_load_by_uuid() when bad UUID given.
  $this
    ->assertFalse(skinr_skin_load_by_uuid(uuid_generate()), 'Skin configuration object not loaded when using non-existing UUID.');
}