public function SlickCrudTest::testOptionSetCrud in Slick Carousel 7.3
Tests Slick optionset CRUD.
File
- tests/
SlickCrudTest.test, line 91
Class
- SlickCrudTest
- Tests the Slick optionsets, configuration options and permission controls.
Code
public function testOptionSetCrud() {
// Login as the admin user.
$this
->drupalLogin($this->adminUser);
$testsets = [
'testset1',
'testset2',
];
foreach ($testsets as $name) {
// Create a new optionset with default settings.
$optionset = Slick::create([
'name' => $name,
]);
$this
->assertTrue($optionset->name == $name, t('Optionset object created: <strong>@name</strong>', [
'@name' => $optionset->name,
]));
$this
->assertFalse(empty($optionset->options), 'Create optionset works.');
// Save the new optionset to the database.
$optionset
->save();
$this
->assertFalse(FALSE === $optionset, 'Optionset saved to database.');
// Read the values from the database.
$optionset = Slick::load($name);
$this
->assertTrue($optionset instanceof Slick, t('Loaded optionset: <strong>@name</strong> instanceof Slick', [
'@name' => $optionset->name,
]));
$this
->assertEqual($name, $optionset->name, t('Loaded name matches: <strong>@name1 == @name2</strong>', [
'@name1' => $name,
'@name2' => $optionset->name,
]));
// Ensure defaults match the custom saved data when no overrides.
$default = Slick::load('default');
foreach ($default->options['settings'] as $key => $value) {
$new_value = $optionset
->getSetting($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>.', [
'@key' => $key,
'@value' => $read_value,
'@new_value' => $read_new_value,
]);
$this
->assertEqual($value, $new_value, $message);
}
}
// Load all optionsets with a reset.
$optionsets = Slick::loadMultiple(TRUE);
$this
->assertTrue(is_array($optionsets), 'Available optionsets loaded');
$message = t('Proper number of optionsets loaded (two created, one default): <strong>@count</strong>', [
'@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>', [
'@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>', [
'@key' => $key,
'@label' => $optionset->label,
]));
$this
->assertTrue(isset($optionset->options), t('<strong>@key</strong>: Loaded optionsets have a defined array of options', [
'@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::load($test);
// Compare saved options different from the set2 options.
foreach ($test_options['set2'] as $key => $value) {
$saved_value = $optionset
->getSetting($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>.', [
'@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
->save();
$this
->assertTrue($optionset instanceof Slick, 'Saved updates to optionset instanceof Slick to database.');
// Load the values from the database again.
$optionset = Slick::load($test);
// Compare saved options match the set2 options.
foreach ($test_options['set2'] as $key => $value) {
$saved_value = $optionset
->getSetting($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>.', [
'@key' => $key,
'@saved_value' => $read_saved_value,
'@value' => $read_value,
]);
$this
->assertEqual($saved_value, $value, $message);
}
// Delete the optionset.
$this
->assertTrue(Slick::exists($test), t('Optionset <strong>@name</strong> exists and is ready to be deleted and reverted.', [
'@name' => $test,
]));
// Remove from db, but kept in code with Slick::create() above.
$optionset
->delete();
// Ensure the optionset is reverted to code after deletion from DB.
$this
->assertTrue(Slick::exists($test), t('Optionset <strong>@name</strong> is deleted from DB, and reverted to code.', [
'@name' => $test,
]));
}