View source
<?php
class BoxesTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => t('Boxes functionality'),
'description' => t('Add and delete custom boxes.'),
'group' => t('Boxes'),
);
}
function setUp() {
parent::setUp('ctools', 'boxes');
$admin_user = $this
->drupalCreateUser(array(
'administer blocks',
));
$this
->drupalLogin($admin_user);
}
function testBoxes() {
$box = array();
$box['description'] = $this
->randomName(8);
$box['title'] = $this
->randomName(8);
$box['body'] = $this
->randomName(32);
$box['delta'] = strtolower($this
->randomName(16));
$this
->drupalPost('admin/build/block/box-add/simple', $box, t('Save'));
$this
->assertText(t('@description has been created.', array(
'@description' => $box['description'],
)), t('Box successfully created.'));
$delta = db_result(db_query("select delta from {box} where delta = '%s'", array(
$box['delta'],
)));
$this
->assertnotnull($delta, t('box found in database'));
$this
->drupalPost('admin/build/block/configure/boxes/' . $delta . '/delete/', array(), t('Delete'));
$delta = db_result(db_query("select delta from {box} where delta = '%s'", array(
$box['delta'],
)));
$this
->assertFalse($delta, t('box not found in database'));
}
}
class BoxesAjaxTestCase extends DrupalWebTestCase {
function parseJSON() {
$json = str_replace(array(
'\\x3c',
'\\x3e',
'\\x26',
), array(
"<",
">",
"&",
), $this->content);
$json = str_replace(array(
"\\'",
), array(
"'",
), $json);
$json = json_decode($json);
if (json_last_error() == JSON_ERROR_NONE) {
$this
->pass("Parsed JSON response");
}
else {
$this
->fail("Failed to parse JSON response");
}
return $json;
}
function ajaxLoadBoxesBlock($delta, $path = 'node') {
$this
->drupalGet($path, array(
'query' => array(
'boxes_delta' => $delta,
),
));
$response = $this
->parseJSON();
$block = null;
foreach ($response as $command) {
if ($command->command == 'replace') {
$block = $command->data;
break;
}
}
if ($block) {
$this
->pass("Loaded block");
$this->content = $block;
unset($this->elements);
}
else {
$this
->fail('Failed to load block');
}
}
}
class BoxesBasicAjaxTestCase extends BoxesAjaxTestCase {
public static function getInfo() {
return array(
'name' => t('Boxes Ajax functionality'),
'description' => t('Add a custom boxes with AJAX.'),
'group' => t('Boxes'),
);
}
function setUp() {
parent::setUp('ctools', 'context', 'boxes');
$admin_user = $this
->drupalCreateUser(array(
'administer blocks',
));
$this
->drupalLogin($admin_user);
}
function testAjaxBoxes() {
$this
->ajaxLoadBoxesBlock('boxes_add__simple');
$this
->assertText(t('Add custom box'), 'Found box add form');
$edit = array(
'description' => $this
->randomName(),
'title' => $this
->randomName(),
'body' => $this
->randomName(32),
);
$this
->drupalPost(NULL, $edit, t('Save'));
$response = $this
->parseJSON();
$delta = null;
foreach ($response as $command) {
if ($command->command == 'getBlock') {
$delta = $command->delta;
break;
}
}
if (!$delta) {
$this
->fail('AJAX block submission failed');
}
$this
->ajaxLoadBoxesBlock($delta);
$this
->assertText($edit['title'], 'Found box');
}
}
class BoxesSpacesTestCase extends BoxesAjaxTestCase {
public static function getInfo() {
return array(
'name' => t('Boxes Spaces functionality'),
'description' => t('Add custom boxes in Spaces.'),
'group' => t('Boxes'),
);
}
function setUp() {
parent::setUp('ctools', 'boxes', 'features', 'purl', 'spaces', 'spaces_ui', 'spaces_user', 'taxonomy', 'spaces_taxonomy');
$admin_user = $this
->drupalCreateUser(array(
'administer blocks',
'administer spaces',
'administer site configuration',
'administer taxonomy',
));
$this
->drupalLogin($admin_user);
}
function runTest($path) {
$this
->ajaxLoadBoxesBlock('boxes_add__simple', $path);
$this
->assertResponse('200', 'Response code 200');
$this
->assertText(t('Add custom box'), 'Found box add form');
$edit = array(
'description' => $this
->randomName(),
'title' => $this
->randomName(),
'body' => $this
->randomName(32),
);
$this
->drupalPost(NULL, $edit, t('Save'));
$response = $this
->parseJSON();
$delta = null;
foreach ($response as $command) {
if ($command->command == 'getBlock') {
$delta = $command->delta;
break;
}
}
if (!$delta) {
$this
->fail('AJAX block submission failed');
}
$this
->ajaxLoadBoxesBlock($delta, $path);
$this
->assertResponse('200', 'Response code 200');
$this
->assertText($edit['title'], 'Found box');
$this
->ajaxLoadBoxesBlock($delta, 'node');
$this
->assertNoText($edit['title'], "Block not available outside spaces.");
return $delta;
}
function testUserSpace() {
$delta = $this
->runTest('user/3');
$this
->drupalPost('admin/settings/performance', array(), t('Clear cached data'));
$this
->drupalGet('user/3/features/overrides');
$this
->assertResponse('200', 'Response code 200');
$this
->assertText($delta, 'Found overridden box: ' . $delta);
}
function testTermSpace() {
$edit = array(
'purl_types[path]' => 'path',
);
$this
->drupalPost('admin/settings/purl/types', $edit, t('Save configuration'));
$edit = array(
'purl_method_spaces_taxonomy' => 'path',
);
$this
->drupalPost('admin/settings/purl', $edit, t('Save configuration'));
$edit = array(
'name' => $this
->randomName(),
'module' => strtolower($this
->randomName()),
);
$this
->drupalPost('admin/content/taxonomy/add/vocabulary', $edit, t('Save'));
$edit = array(
'spaces_taxonomy_vid' => '1',
);
$this
->drupalPost('admin/build/spaces/taxonomy', $edit, t('Save configuration'));
$edit = array(
'name' => $this
->randomName(),
'purl[value]' => strtolower($this
->randomName()),
);
$this
->drupalPost('admin/content/taxonomy/1/add/term', $edit, t('Save'));
$prefix = $edit['purl[value]'];
$this
->drupalGet($prefix . '/node');
$this
->assertResponse('200', 'Response code 200');
$delta = $this
->runTest($prefix . '/node');
$this
->drupalPost('admin/settings/performance', array(), t('Clear cached data'));
$this
->drupalGet('taxonomy/term/1/features/overrides');
$this
->assertResponse('200', 'Response code 200');
$this
->assertText($delta, 'Found overridden box: ' . $delta);
}
}