You are here

public function SkinrApiTest::_testSkinrSkinLoadSave in Skinr 8.2

Test skinr_skin_save() against invalid entries.

File

src/Tests/SkinrApiTest.php, line 359
Contains \Drupal\skinr\Tests\SkinrApiTest.

Class

SkinrApiTest
Tests Skinr API functionality.

Namespace

Drupal\skinr\Tests

Code

public function _testSkinrSkinLoadSave() {

  // Only save valid skins.
  $skin = Skin::create(array(
    'theme' => '',
    'element_type' => 'block',
    'element' => 'system__user-menu',
    'skin' => 'skinr_test_subtheme',
    'options' => array(
      'option1' => 'option1',
      'option2' => 'option2',
    ),
    'status' => 1,
  ));
  $this
    ->assertFalse($skin
    ->save(), 'Skin configuration object not saved when $skin->theme is empty.');
  $skin->theme = 'skinr_test_subtheme';
  $skin->element_type = '';
  $this
    ->assertFalse($skin
    ->save(), 'Skin configuration object not saved when $skin->element_type is empty.');
  $skin->element_type = 'block';
  $skin->element = '';
  $this
    ->assertFalse($skin
    ->save(), 'Skin configuration object not saved when $skin->element is empty.');
  $skin->element = 'system-user-menu';
  $skin->skin = '';
  $this
    ->assertFalse($skin
    ->save(), 'Skin configuration object not saved when $skin->skin is empty.');
  $skin->skin = 'skinr_test_subtheme';
  $skin
    ->setOptions('');
  $this
    ->assertFalse($skin
    ->save(), 'Skin configuration object not saved when $skin->options is not array.');
  $skin
    ->setOptions(array());
  $this
    ->assertFalse($skin
    ->save(), 'Skin configuration object saved when $skin->options is empty array.');
  $skin
    ->setOptions(array(
    'option1' => 0,
    'option2' => 0,
  ));
  $this
    ->assertFalse($skin
    ->save(), 'Skin configuration object not saved when $skin->options is complex empty array.');
  $skin
    ->setOptions(array(
    'option1' => 'option1',
    'option2' => 'option2',
  ));
  $this
    ->assertTrue($skin
    ->save(), 'Skin configuration object was saved.');
  $this
    ->assertTrue($skin
    ->id(), 'ID added to skin configuration object.');
  $this
    ->assertTrue(Uuid::isValid($skin
    ->id()), 'ID for skin configuration object is valid.');

  // Test loading a skin configuration.

  /* @var Skin $loaded_skin */
  $loaded_skin = Skin::load($skin
    ->id());
  $this
    ->assertTrue(is_array($loaded_skin
    ->getOptions()), 'Options for skin configuration object are unserialized.');
  $this
    ->assertTrue($loaded_skin->theme == $skin->theme && $loaded_skin->element_type == $skin->element_type && $loaded_skin->element == $skin->element && $loaded_skin
    ->status() == $skin
    ->status() && $loaded_skin
    ->getOption(0) == $skin
    ->getOption(0) && $loaded_skin
    ->getOption(1) == $skin
    ->getOption(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.
  $second_skin = Skin::create(array(
    'uuid' => \Drupal::service('uuid')
      ->generate(),
    'theme' => 'skinr_test_subtheme',
    'element_type' => 'block',
    'element' => 'system__main',
    'skin' => 'skinr_test_subtheme',
    'options' => array(
      'option3' => 'option3',
    ),
    'status' => 1,
  ));
  skinr_skin_save($second_skin);

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

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

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

  // Test skinr_skin_uuid_to_sid().
  $this
    ->assertTrue(skinr_skin_uuid_to_sid($second_skin
    ->id()) == $second_skin
    ->id(), '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
    ->id()) == $second_skin
    ->id(), '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
    ->id());
  $this
    ->assertTrue($loaded_skin
    ->id() == $second_skin
    ->id(), 'Skin configuration object loaded using UUID.');

  // Test skinr_skin_load_by_uuid() when bad UUID given.
  $this
    ->assertFalse(skinr_skin_load_by_uuid(\Drupal::service('uuid')
    ->generate()), 'Skin configuration object not loaded when using non-existing UUID.');
}