View source
<?php
namespace Drupal\optimizely\Tests;
use Drupal\Core\Language\LanguageInterface;
use Drupal\simpletest\WebTestBase;
class OptimizelyEnableDisableTest extends WebTestBase {
protected $addUpdatePage = 'admin/config/system/optimizely/add_update';
protected $update2Page = 'admin/config/system/optimizely/add_update/2';
protected $addAliasPage = 'admin/config/search/path/add';
protected $privilegedUser;
protected $optimizelyPermission = 'administer optimizely';
public static $modules = [
'optimizely',
'node',
'path',
];
public static function getInfo() {
return [
'name' => 'Optimizely Enable / Disable Project',
'description' => 'Test enabling / disabling non-default projects.',
'group' => 'Optimizely',
];
}
public function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this->privilegedUser = $this
->drupalCreateUser([
'access content',
'create page content',
'administer url aliases',
'create url aliases',
$this->optimizelyPermission,
]);
}
public function testEnableDisable() {
$this
->drupalLogin($this->privilegedUser);
$settings = [
'type' => 'page',
'title' => $this
->randomMachineName(32),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'body' => [
[
'value' => $this
->randomMachineName(64),
'format' => filter_default_format(),
],
],
];
$node = $this
->drupalCreateNode($settings);
$edit_node = [];
$edit_node['source'] = '/node/' . $node
->id();
$edit_node['alias'] = '/' . $this
->randomMachineName(10);
$this
->drupalPostForm($this->addAliasPage, $edit_node, t('Save'));
$edit = [
'optimizely_project_title' => $this
->randomMachineName(8),
'optimizely_project_code' => mt_rand(0, 10000),
'optimizely_path' => $edit_node['alias'],
'optimizely_enabled' => 0,
];
$this
->drupalPostForm($this->addUpdatePage, $edit, t('Add'));
$edit_2 = [
'optimizely_enabled' => 1,
];
$this
->drupalPostForm($this->update2Page, $edit_2, t('Update'));
$enabled = \Drupal::database()
->query('SELECT enabled FROM {optimizely} WHERE oid = 2')
->fetchField();
$this
->assertEqual($enabled, $edit_2['optimizely_enabled'], t('<strong>The project was enabled from update page.</strong>'), 'Optimizely');
$edit_3 = [
'optimizely_enabled' => 0,
];
$this
->drupalPostForm($this->update2Page, $edit_3, t('Update'));
$enabled = \Drupal::database()
->query('SELECT enabled FROM {optimizely} WHERE oid = 2')
->fetchField();
$this
->assertEqual($enabled, $edit_3['optimizely_enabled'], t('<strong>The project was disabled from update page.</strong>'), 'Optimizely');
}
}