public function FlexsliderTest::testOptionSetCrud in Flex Slider 8.2
Test managing the optionset.
File
- tests/
src/ Functional/ FlexsliderTest.php, line 86
Class
- FlexsliderTest
- Test the FlexSlider presets, configuration options and permission controls.
Namespace
Drupal\Tests\flexslider\FunctionalCode
public function testOptionSetCrud() {
// Login as the admin user.
$this
->drupalLogin($this->adminUser);
$testsets = [
'testset',
'testset2',
];
foreach ($testsets as $name) {
// Create a new optionset with default settings.
/** @var \Drupal\flexslider\Entity\Flexslider $optionset */
$optionset = Flexslider::create([
'id' => $name,
'label' => $name,
]);
$this
->assertNotEmpty($optionset
->id() == $name, $this
->t('Optionset object created: @name', [
'@name' => $optionset
->id(),
]));
$this
->assertNotEmpty($optionset
->getOptions(), $this
->t('Create optionset works.'));
// Save the optionset to the database.
$optionset
->save();
$this
->assertNotEmpty($optionset, $this
->t('Optionset saved to database.'));
// Read the values from the database.
$optionset = Flexslider::load($name);
$this
->assertIsObject($optionset, $this
->t('Loaded option set.'));
$this
->assertEqual($name, $optionset
->id(), $this
->t('Loaded name matches: @name', [
'@name' => $optionset
->id(),
]));
/** @var \Drupal\flexslider\Entity\Flexslider $default_optionset */
$default_optionset = Flexslider::create();
foreach ($default_optionset
->getOptions() as $key => $value) {
$this
->assertEqual($value, $optionset
->getOptions()[$key], $this
->t('Option @option matches saved value.', [
'@option' => $key,
]));
}
}
// Load all optionsets.
$optionsets = Flexslider::loadMultiple();
$this
->assertIsArray($optionsets, $this
->t('Array of optionsets loaded'));
$this
->assertNotEmpty(count($optionsets) == 3, $this
->t('Proper number of optionsets loaded (two created, one default): 3'));
// Ensure they all loaded correctly.
foreach ($optionsets as $optionset) {
$this
->assertNotEmpty($optionset
->id(), $this
->t('Loaded optionsets have a defined machine name'));
$this
->assertNotEmpty($optionset
->label(), $this
->t('Loaded optionsets have a defined human readable name (label)'));
$this
->assertTrue(!empty($optionset
->getOptions()), $this
->t('Loaded optionsets have a defined array of options'));
}
// Update the optionset.
$test_options = $this
->getTestOptions();
$test_options = $test_options['valid'];
// Load one of the test option sets.
$optionset = Flexslider::load($testsets[0]);
// Change the settings.
$optionset
->setOptions($test_options['set2'] + $optionset
->getOptions());
// Save the updated values.
$saved = $optionset
->save();
$this
->assertEqual($saved, SAVED_UPDATED, $this
->t('Saved updates to optionset to database.'));
// Load the values from the database again.
$optionset = Flexslider::load($testsets[0]);
// Compare settings to the test options.
foreach ($test_options['set2'] as $key => $value) {
$this
->assertEqual($optionset
->getOptions()[$key], $value, $this
->t('Saved value matches set value: @key', [
'@key' => $key,
]));
}
// Delete the optionset.
$this
->assertIsObject($optionset, $this
->t('Optionset exists and is ready to be deleted.'));
try {
$optionset
->delete();
// Ensure the delete is successful.
$this
->pass($this
->t('Optionset successfully deleted: @name', [
'@name' => $optionset
->id(),
]));
} catch (\Exception $e) {
$this
->fail($this
->t('Caught exception: @msg', [
'@msg' => $e
->getMessage(),
]));
}
}