View source
<?php
use Drupal\slick\Entity\Slick;
class SlickCrudTest extends DrupalWebTestCase {
protected $adminUser;
protected $anyUser;
public static function getInfo() {
return [
'name' => 'Slick tests',
'description' => 'Tests the Slick optionsets, configuration options and permission controls.',
'group' => 'Slick',
];
}
public function setUp() {
parent::setUp('libraries', 'ctools', 'jquery_update', 'registry_autoload', 'blazy', 'slick', 'slick_ui');
$this->profile = 'testing';
$this->adminUser = $this
->drupalCreateUser([
'administer slick',
]);
$this->anyUser = $this
->drupalCreateUser([
'access administration pages',
]);
}
public function testAdminAccess() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/media/slick');
$this
->assertResponse(200, 'Administrative permission allows access to administration page.');
$this
->drupalLogout();
$this
->drupalLogin($this->anyUser);
$this
->drupalGet('admin/config/media/slick');
$this
->assertResponse(403, 'Regular users do not have access to administer Slick pages.');
}
public function testOptionSetCrud() {
$this
->drupalLogin($this->adminUser);
$testsets = [
'testset1',
'testset2',
];
foreach ($testsets as $name) {
$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.');
$optionset
->save();
$this
->assertFalse(FALSE === $optionset, 'Optionset saved to 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,
]));
$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);
}
}
$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);
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,
]));
}
$test_options = $this
->getOptions();
$test_options = $test_options['valid'];
$test = $testsets[1];
$optionset = Slick::load($test);
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);
}
$optionset->options['settings'] = (array) $test_options['set2'] + (array) $optionset->options['settings'];
$optionset
->save();
$this
->assertTrue($optionset instanceof Slick, 'Saved updates to optionset instanceof Slick to database.');
$optionset = Slick::load($test);
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);
}
$this
->assertTrue(Slick::exists($test), t('Optionset <strong>@name</strong> exists and is ready to be deleted and reverted.', [
'@name' => $test,
]));
$optionset
->delete();
$this
->assertTrue(Slick::exists($test), t('Optionset <strong>@name</strong> is deleted from DB, and reverted to code.', [
'@name' => $test,
]));
}
public function testOptionSetForm() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/media/slick/add');
$this
->assertResponse(200, 'Administrative user can reach the "Add" form.');
$optionset = [];
$optionset['label'] = 'Testset';
$optionset['name'] = 'testset';
$this
->drupalPost('admin/config/media/slick/add', $optionset, t('Save'));
$this
->assertResponse(200);
$this
->assertTrue(Slick::exists($optionset['name']), t('Successfully created the new optionset: @label', [
'@label' => $optionset['label'],
]));
$this
->drupalPost('admin/config/media/slick/add', $optionset, t('Save'));
$this
->assertResponse(200);
$this
->assertText("The machine-readable name is already in use. It must be unique.", "Blocked the creation of duplicate named optionset.");
$options = $this
->getOptions();
foreach ($options['valid'] as $edit) {
$xpath = [];
foreach ($edit as $key => $value) {
$xpath["options[settings][{$key}]"] = $value;
}
$this
->drupalPost('admin/config/media/slick/list/default/edit', $xpath, t('Save'));
$this
->assertResponse(200, 'Default optionset overriden.');
$this
->drupalGet('admin/config/media/slick/list/default/edit');
$this
->assertResponse(200, 'Default optionset reloaded.');
foreach ($edit as $v => $value) {
$read_value = $this
->getPrintedValue($value);
$this
->assertFieldByName("options[settings][{$v}]", $value, t("@v:<strong>@value</strong> inserted correctly.", [
'@value' => $read_value,
'@v' => $v,
]));
}
}
$testset = Slick::load('testset');
$this
->drupalGet("admin/config/media/slick/list/{$testset->name}/delete");
$this
->assertResponse(200);
$this
->assertText("Are you sure you want to delete {$testset->name}?", 'Delete confirmation form loaded.');
$this
->drupalPost("admin/config/media/slick/list/{$testset->name}/delete", '', 'Delete');
$this
->assertResponse(200);
$this
->assertText("The item has been deleted.", 'Deleted Testset using form. Done testing!');
}
public function getOptions() {
$defaults = Slick::defaultSettings();
$valid = [
'set1' => $defaults,
'set2' => [
'autoplay' => TRUE,
'initialSlide' => 1,
],
];
$error = [];
return [
'valid' => $valid,
'error' => $error,
];
}
public function getPrintedValue($value) {
$read_value = $value === FALSE ? 'FALSE' : ($value === TRUE ? 'TRUE' : $value);
$read_value = empty($read_value) ? 'NULL' : $read_value;
return $read_value;
}
}