View source
<?php
class SpacesVariableControllerTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => t('Controller: variable'),
'description' => t('Unit test for variable controller.'),
'group' => t('Spaces'),
);
}
public function setUp() {
parent::setUp('ctools', 'features', 'spaces_user', 'spaces');
}
public function test() {
ctools_include('export');
$preset = ctools_export_new_object('spaces_presets');
$preset->name = 'testpreset';
$preset->title = 'Test preset';
$preset->space_type = 'user';
$preset->value = array(
'variable' => array(
'foo' => 'bar',
),
);
spaces_preset_save($preset);
variable_set('foo', 'foo');
spaces_types(TRUE);
drupal_static_reset('ctools_plugins');
ctools_get_plugins_reset();
$space = spaces_load('user', 0);
if ($space == FALSE) {
$this
->fail('Could not load user space');
return;
}
else {
$this
->pass('Loaded user space');
}
$space
->activate();
$this
->assertTrue($space->controllers->variable
->get('foo') === 'foo', t('Inheritance (Original)'));
$space->controllers->variable
->set('spaces_preset_user', 'testpreset');
$space
->init_overrides();
$this
->assertTrue($space->controllers->variable
->get('foo') === 'bar', t('Inheritance (Original > Preset)'));
$space->controllers->variable
->set('foo', 'baz');
$this
->assertTrue($space->controllers->variable
->get('foo') === 'baz', t('Inheritance (Original > Preset > Space)'));
$space->controllers->variable
->del('spaces_preset_user');
$space
->init_overrides();
$this
->assertTrue($space->controllers->variable
->get('foo') === 'baz', t('Inheritance (Original > Space)'));
$space->controllers->variable
->set('spaces_preset_user', 'testpreset');
$space
->init_overrides();
$this
->assertTrue($space->controllers->variable
->get('foo', 'original') === 'foo', t('Environment (Original)'));
$this
->assertTrue($space->controllers->variable
->get('foo', 'preset') === 'bar', t('Environment (Preset)'));
$this
->assertTrue($space->controllers->variable
->get('foo', 'space') === 'baz', t('Environment (Space)'));
variable_del('foo');
spaces_delete('user', 0);
spaces_preset_delete('testpreset');
}
}
class SpacesContextControllerTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => t('Controller: context'),
'description' => t('Unit test for context controller.'),
'group' => t('Spaces'),
);
}
public function setUp() {
parent::setUp('context', 'ctools', 'features', 'spaces', 'spaces_user');
}
public function test() {
ctools_include('export');
$context = ctools_export_new_object('context');
$context->name = 'testcontext';
$context->conditions = array(
'path' => array(
'values' => array(
'<front>',
),
),
);
$context->reactions = array();
$saved = context_save($context);
$this
->assertTrue($saved, "Context 'testcontext' saved.");
$preset = ctools_export_new_object('spaces_presets');
$preset->name = 'testpreset';
$preset->title = 'Test preset';
$preset->space_type = 'user';
$preset->value = array(
'context' => array(
'testcontext:condition:path' => array(
'values' => array(
'node',
),
),
),
);
spaces_preset_save($preset);
spaces_types(TRUE);
drupal_static_reset('ctools_plugins');
ctools_get_plugins_reset();
$space = spaces_load('user', 0);
if ($space == FALSE) {
$this
->fail('Could not load user space');
return;
}
else {
$this
->pass('Loaded user space');
}
$space
->activate();
$this
->assertTrue($space->controllers->context
->get('testcontext:condition:path') === array(
'values' => array(
'<front>',
),
), t('Inheritance (Original)'));
$space->controllers->variable
->set('spaces_preset_user', 'testpreset');
$space
->init_overrides();
$this
->assertTrue($space->controllers->context
->get('testcontext:condition:path') === array(
'values' => array(
'node',
),
), t('Inheritance (Original > Preset)'));
$space->controllers->context
->set('testcontext:condition:path', array(
'values' => array(
'user',
),
));
$this
->assertTrue($space->controllers->context
->get('testcontext:condition:path') === array(
'values' => array(
'user',
),
), t('Inheritance (Original > Preset > Space)'));
$space->controllers->variable
->del('spaces_preset_user');
$space
->init_overrides();
$this
->assertTrue($space->controllers->context
->get('testcontext:condition:path') === array(
'values' => array(
'user',
),
), t('Inheritance (Original > Space)'));
$space->controllers->variable
->set('spaces_preset_user', 'testpreset');
$space
->init_overrides();
$this
->assertTrue($space->controllers->context
->get('testcontext:condition:path', 'original') === array(
'values' => array(
'<front>',
),
), t('Environment (Original)'));
$this
->assertTrue($space->controllers->context
->get('testcontext:condition:path', 'preset') === array(
'values' => array(
'node',
),
), t('Environment (Preset)'));
$this
->assertTrue($space->controllers->context
->get('testcontext:condition:path', 'space') === array(
'values' => array(
'user',
),
), t('Environment (Space)'));
context_delete('testcontext');
spaces_delete('user', 0);
spaces_preset_delete(spaces_preset_load('testpreset'));
}
}
class SpacesTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => t('Spaces'),
'description' => t('Test access control and simple feature settings using as example the "Feature test" feature that ships with Features.'),
'group' => t('Spaces'),
);
}
public function setUp() {
parent::setUp('ctools', 'features', 'features_test', 'spaces', 'spaces_test');
}
public function test() {
$admin = $this
->drupalCreateUser(array(
'access content',
'administer nodes',
'administer content types',
'administer site configuration',
'administer spaces',
));
$editor = $this
->drupalCreateUser(array(
'access content',
'create features_test content',
'edit own features_test content',
'delete own features_test content',
));
$this
->drupalLogin($editor);
$this
->drupalGet('node/add');
$this
->assertNoRaw('node/add/features-test', 'Create content link not available');
$this
->drupalLogin($admin);
$edit = array(
'spaces_features[features_test]' => '1',
);
$this
->drupalPost('features', $edit, t('Save configuration'));
$this
->assertText('The configuration options have been saved.');
$this
->drupalLogin($editor);
$this
->drupalGet('node/add');
$this
->assertRaw('node/add/features-test', 'Create content link is available');
$edit = array(
'title' => $this
->randomName(10),
'field_features_test[und][0][value]' => $this
->randomName(20),
);
$this
->drupalPost('node/add/features-test', $edit, 'Save');
$this
->assertText('has been created');
$this
->assertNoText('Save');
$this
->drupalLogin($admin);
$edit = array(
'spaces_features[features_test]' => '0',
);
$this
->drupalPost('features', $edit, 'Save configuration');
$this
->assertText('The configuration options have been saved.');
$this
->drupalLogin($editor);
$this
->drupalGet('node/add/features-test');
$this
->assertResponse(403);
$this
->drupalGet('node/1/edit');
$this
->assertResponse(403);
$this
->drupalLogin($admin);
$this
->drupalPost('features/features_test', array(
'features_test_setting' => '1',
), 'Save configuration');
$this
->assertText('The configuration options have been saved.');
}
}
class SpacesMenuTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => t('Spaces Menu'),
'description' => t('Test menu re-ordering.'),
'group' => t('Spaces'),
);
}
public function setUp() {
parent::setUp('ctools', 'features', 'features_test', 'spaces');
$admin_user = $this
->drupalCreateUser(array(
'administer site configuration',
'administer blocks',
'administer menu',
));
$this
->drupalLogin($admin_user);
}
function getMenuOrder() {
$base_path = base_path();
$elems = $this
->xpath('//ul[@id="main-menu-links"] /li /a');
$links = array();
foreach ($elems as $e) {
foreach ($e
->attributes() as $k => $v) {
if ($k == 'href') {
$links[] = $base_path . substr($v . '', 1);
}
}
}
return $links;
}
function testMenuOrdering() {
$edit = array(
"blocks[spaces_menu_editor][region]" => 'sidebar_first',
);
$this
->drupalPost('admin/structure/block', $edit, t('Save blocks'));
$this
->assertText(t('The block settings have been updated.'), t('Block successfully moved to left region.'));
$this
->assertText(t('Reorder menu'), t('Block successfully being displayed on the page.'));
$edit = array();
$edit['menu_main_links_source'] = 'navigation';
$this
->drupalPost('admin/structure/menu/settings', $edit, t('Save configuration'));
$links = $this
->getMenuOrder();
$edit = array(
'space_menu_items' => json_encode(array_reverse($links)),
);
$this
->drupalPost('node', $edit, t('Save'));
$this
->assertText(t('The configuration options have been saved.'), t('Menu order successfully saved.'));
$changed = $this
->getMenuOrder();
$this
->assert($changed == array_reverse($links), t('Menu changes successfully displayed'));
}
}
class ViewsAccessTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => t('Spaces Views Access'),
'description' => t('Test the Spaces access plugin for views.'),
'group' => t('Spaces'),
);
}
public function setUp() {
parent::setUp('php', 'ctools', 'features', 'features_test', 'spaces', 'spaces_test', 'views', 'views_ui');
$this->basic_user = $this
->drupalCreateUser(array(
'access content',
));
$this->admin_user = $this
->drupalCreateUser(array(
'administer site configuration',
'administer blocks',
'administer menu',
'administer views',
'use PHP for settings',
));
$this
->drupalLogin($this->admin_user);
$this
->drupalGet('admin/structure/views/view/comments_recent/export');
$elems = $this
->xpath('//textarea[@id="edit-code"]');
$view_code = explode("\n", (string) $elems[0]);
foreach ($view_code as $n => $line) {
if (strpos($line, '$handler = $view->new_display(\'page\'') === 0) {
$mark = $n + 1;
break;
}
}
$insert = '$handler->display->display_options["defaults"]["access"] = FALSE;
$handler->display->display_options["access"]["type"] = "spaces_feature";
$handler->display->display_options["access"]["spaces_feature"] = "spaces_test";
$handler->display->display_options["access"]["perm"] = "administer site configuration";';
array_splice($view_code, $mark, 0, $insert);
$edit = array(
'name' => 'comments_recent_spaces',
'view' => implode("\n", $view_code),
);
$this
->drupalPost('admin/structure/views/import', $edit, t('Import'));
$this
->drupalPost(null, array(), t('Save'));
}
public function testAccessPlugin() {
$this
->drupalLogin($this->admin_user);
$this
->drupalGet('comments/recent');
$this
->assertResponse('200', t('Authorized user allowed'));
$this
->drupalLogin($this->basic_user);
$this
->drupalGet('comments/recent');
$this
->assertResponse('403', t('Unauthorized user blocked'));
}
}