View source
<?php
namespace Drupal\Tests\flexslider\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\flexslider\Entity\Flexslider;
use Drupal\flexslider\FlexsliderDefaults;
use Drupal\Tests\BrowserTestBase;
class FlexsliderTest extends BrowserTestBase {
use StringTranslationTrait;
public static $modules = [
'flexslider',
'flexslider_library_test',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $anyUser;
public function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer flexslider',
], NULL, TRUE);
$this->anyUser = $this
->drupalCreateUser([
'access administration pages',
]);
}
public function testAdminAccess() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/media/flexslider');
$this
->assertResponse(200);
$this
->drupalLogout();
$this
->drupalLogin($this->anyUser);
$this
->drupalGet('admin/config/media/flexslider');
$this
->assertResponse(403);
}
public function testOptionSetCrud() {
$this
->drupalLogin($this->adminUser);
$testsets = [
'testset',
'testset2',
];
foreach ($testsets as $name) {
$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.'));
$optionset
->save();
$this
->assertNotEmpty($optionset, $this
->t('Optionset saved to 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(),
]));
$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,
]));
}
}
$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'));
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'));
}
$test_options = $this
->getTestOptions();
$test_options = $test_options['valid'];
$optionset = Flexslider::load($testsets[0]);
$optionset
->setOptions($test_options['set2'] + $optionset
->getOptions());
$saved = $optionset
->save();
$this
->assertEqual($saved, SAVED_UPDATED, $this
->t('Saved updates to optionset to database.'));
$optionset = Flexslider::load($testsets[0]);
foreach ($test_options['set2'] as $key => $value) {
$this
->assertEqual($optionset
->getOptions()[$key], $value, $this
->t('Saved value matches set value: @key', [
'@key' => $key,
]));
}
$this
->assertIsObject($optionset, $this
->t('Optionset exists and is ready to be deleted.'));
try {
$optionset
->delete();
$this
->pass($this
->t('Optionset successfully deleted: @name', [
'@name' => $optionset
->id(),
]));
} catch (\Exception $e) {
$this
->fail($this
->t('Caught exception: @msg', [
'@msg' => $e
->getMessage(),
]));
}
}
public function testOptionSetForm() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/media/flexslider/add');
$this
->assertResponse(200);
$optionset = [];
$optionset['label'] = $this
->t('testset');
$optionset['id'] = 'testset';
$this
->drupalPostForm('admin/config/media/flexslider/add', $optionset, $this
->t('Save'));
$this
->assertResponse(200);
$this
->assertText('Created the testset FlexSlider optionset.');
$this
->drupalPostForm('admin/config/media/flexslider/add', $optionset, $this
->t('Save'));
$this
->assertResponse(200);
$this
->assertText('The machine-readable name is already in use. It must be unique.');
$options = $this
->getTestOptions();
foreach ($options['valid'] as $testset) {
$this
->drupalPostForm('admin/config/media/flexslider/default', $testset, $this
->t('Save'));
$this
->assertResponse(200);
$this
->drupalGet('admin/config/media/flexslider/default');
$this
->assertResponse(200);
foreach ($testset as $key => $option) {
$this
->assertFieldByName($key, $option);
}
}
$testset = Flexslider::load('testset');
$this
->drupalGet("admin/config/media/flexslider/{$testset->id()}/delete");
$this
->assertResponse(200);
$this
->assertText("Are you sure you want to delete {$testset->label()}?");
$this
->drupalPostForm("admin/config/media/flexslider/{$testset->id()}/delete", [], $this
->t('Delete'));
$this
->assertResponse(200);
$this
->assertText("Deleted the {$testset->label()} FlexSlider optionset.");
}
public function testSettings() {
$this
->drupalLogin($this->adminUser);
$this
->assertRaw('libraries/flexslider/jquery.flexslider-min.js');
$this
->drupalGet('admin/config/media/flexslider/advanced');
$settings['flexslider_debug'] = TRUE;
$this
->drupalPostForm('admin/config/media/flexslider/advanced', $settings, $this
->t('Save configuration'));
$this
->assertResponse(200);
$this
->assertText('The configuration options have been saved.');
$this
->drupalGet('user/' . $this->adminUser
->id());
$this
->assertRaw('libraries/flexslider/jquery.flexslider.js');
$this
->assertRaw('libraries/flexslider/flexslider.css');
$this
->assertRaw('flexslider/assets/css/flexslider_img.css');
$this
->drupalGet('admin/config/media/flexslider/advanced');
$settings = [
'flexslider_css' => FALSE,
'integration_css' => FALSE,
];
$this
->drupalPostForm('admin/config/media/flexslider/advanced', $settings, $this
->t('Save configuration'));
$this
->drupalGet('user/' . $this->adminUser
->id());
$this
->assertNoRaw('libraries/flexslider/flexslider.css');
$this
->assertNoRaw('flexslider/assets/css/flexslider_img.css');
}
protected function getTestOptions() {
$valid = [
'set1' => FlexsliderDefaults::defaultOptions(),
'set2' => [
'animation' => 'slide',
'startAt' => 4,
],
];
$error = [];
return [
'valid' => $valid,
'error' => $error,
];
}
}