View source
<?php
require_once drupal_get_path('module', 'location') . '/tests/location_testcase.php';
class LocationCCKTest extends LocationTestCase {
var $admin_user;
var $normal_user;
var $content_type;
function getInfo() {
return array(
'name' => t('Location CCK checks'),
'description' => t('Test corner cases of the CCK Location module.'),
'group' => t('Location'),
);
}
function setUp() {
parent::setUp('location', 'content', 'location_cck', 'devel');
$this->admin_user = $this
->drupalCreateUser(array(
'administer nodes',
'submit latitude/longitude',
'administer site configuration',
'access administration pages',
'administer content types',
));
$this->normal_user = $this
->drupalCreateUser(array(
'access content',
));
$this
->drupalLogin($this->admin_user);
}
function addLocationContentType(&$settings, $add = array()) {
$field_name = 'loctest';
$required = isset($add['cck_required']) ? $add['cck_required'] : FALSE;
$multiple = isset($add['cck_multiple']) ? $add['cck_multiple'] : 0;
unset($add['cck_required']);
unset($add['cck_multiple']);
do {
$name = strtolower($this
->randomName(3, 'type_'));
} while (node_get_types('type', $name));
$form = array(
'name' => $name,
'type' => $name,
);
$this
->flattenPostData($form);
$this
->drupalPost('admin/content/types/add', $form, 'Save content type');
$this
->refreshVariables();
$form = array(
'_add_new_field' => array(
'label' => 'Location',
'weight' => 10,
'field_name' => $field_name,
'type' => 'location',
'widget_type' => 'location',
),
);
$this
->flattenPostData($form);
$this
->drupalPost('admin/content/node-type/' . str_replace('_', '-', $name) . '/fields', $form, 'Save');
$this
->refreshVariables();
drupal_get_schema(NULL, TRUE);
$defaults = $this
->getLocationFieldDefaults();
$form = array(
'required' => $required,
'multiple' => $multiple,
'location_settings' => array(
'form' => array(
'fields' => $defaults,
),
),
);
$this
->flattenPostData($form);
$add = array(
'location_settings' => $add,
);
$this
->flattenPostData($add);
$this
->drupalPost(NULL, $form, 'Save field settings');
$result = db_query("SELECT global_settings FROM {content_node_field} WHERE field_name = 'field_%s' AND type = 'location'", $field_name);
$row = db_fetch_object($result);
$settings = array();
$settings = unserialize($row->global_settings);
$settings = $settings['location_settings'];
$this
->refreshVariables();
drupal_get_schema(NULL, TRUE);
_content_type_info(TRUE);
return $name;
}
function testCreateLocation() {
$settings = array();
$location_type = $this
->addLocationContentType($settings);
$location1_name = $this
->randomName();
$node = $this
->drupalCreateNode(array(
'type' => $location_type,
'field_loctest' => array(
array(
'name' => $location1_name,
'street' => '48072 289th St.',
'province' => 'SD',
'country' => 'us',
),
),
));
cache_clear_all();
$node2 = node_load($node->nid, NULL, TRUE);
$location = $node2->field_loctest[0];
$this
->assertEqual($location1_name, $location['name'], t('Testing basic save/load'));
}
function testLocpickOnly() {
$settings = array();
$location_type = $this
->addLocationContentType($settings);
$location1_name = $this
->randomName();
$node = $this
->drupalCreateNode(array(
'type' => $location_type,
'field_loctest' => array(
array(
'locpick' => array(
'user_latitude' => '44.25',
'user_longitude' => '-10.25',
),
),
),
));
$node2 = node_load($node->nid, NULL, TRUE);
$this
->pass(var_export($node2->locations, TRUE));
$this
->assertEqual($node2->field_loctest[0]['latitude'], 44.25, t('Testing coordinate-only save/load'));
}
function testMultipleLocationOnSingleNode() {
$settings = array();
$location_type = $this
->addLocationContentType($settings, array(
'cck_multiple' => 10,
));
$location1_name = $this
->randomName();
$location2_name = $this
->randomName();
$location3_name = $this
->randomName();
$node = $this
->drupalCreateNode(array(
'type' => $location_type,
'field_loctest' => array(
array(
'name' => $location1_name,
),
array(
'name' => $location2_name,
),
array(
'name' => $location3_name,
),
),
));
$node2 = node_load($node->nid, NULL, TRUE);
$this
->assertEqual($location1_name, $node2->field_loctest[0]['name'], t('Testing multi location 1/3'));
$this
->assertEqual($location2_name, $node2->field_loctest[1]['name'], t('Testing multi location 2/3'));
$this
->assertEqual($location3_name, $node2->field_loctest[2]['name'], t('Testing multi location 3/3'));
$this
->assertNotEqual($node2->field_loctest[0]['lid'], $node2->field_loctest[1]['lid'], t('Ensuring location id uniqueness'));
$this
->assertNotEqual($node2->field_loctest[1]['lid'], $node2->field_loctest[2]['lid'], t('Ensuring location id uniqueness'));
$this
->assertNotEqual($node2->field_loctest[2]['lid'], $node2->field_loctest[0]['lid'], t('Ensuring location id uniqueness'));
}
function testSharedLocation() {
$settings = array();
$location_type = $this
->addLocationContentType($settings);
$location1_name = $this
->randomName();
$node = $this
->drupalCreateNode(array(
'type' => $location_type,
'field_loctest' => array(
array(
'name' => $location1_name,
),
),
));
$node = node_load($node->nid, NULL, TRUE);
$location = $node->field_loctest[0];
$node2 = $this
->drupalCreateNode(array(
'type' => $location_type,
'field_loctest' => array(
0 => $location,
),
));
$node2 = node_load($node2->nid, NULL, TRUE);
$this
->assertNotEqual($node->nid, $node2->nid, t('Ensuring nodes are seperate'));
$this
->pass(var_export($node, TRUE));
$this
->pass(var_export($node2, TRUE));
$this
->assertEqual($node->field_loctest[0]['lid'], $node2->field_loctest[0]['lid'], t('Testing shared location'));
$this
->deleteNode($node->nid);
$node2 = node_load($node2->nid, NULL, TRUE);
$this
->assertEqual($node2->field_loctest[0]['lid'], $location['lid'], t('Ensuring shared location is not prematurely garbage collected'));
$this
->deleteNode($node2->nid);
$result = db_query('SELECT * FROM {location} WHERE lid = %d', $location['lid']);
if ($row = db_fetch_object($result)) {
$this
->fail(t('Ensuring shared location is garbage collected'));
}
else {
$this
->pass(t('Ensuring shared location is garbage collected'));
}
}
function testNodeRevisionCOW() {
$settings = array();
$location_type = $this
->addLocationContentType($settings, array(
'cck_multiple' => 10,
));
$location1_name = $this
->randomName();
$location2_name = $this
->randomName();
$location3_name = $this
->randomName();
$node = $this
->drupalCreateNode(array(
'type' => $location_type,
'field_loctest' => array(
0 => array(
'name' => $location1_name,
),
1 => array(
'name' => $location2_name,
),
),
));
$node = node_load($node->nid, NULL, TRUE);
$changes = array(
'revision' => TRUE,
'log' => $this
->randomName(20),
'field_loctest' => array(
0 => array(
'delete_location' => TRUE,
),
2 => array(
'name' => $location3_name,
),
),
);
$this
->flattenPostData($changes);
$this
->drupalPost('node/' . $node->nid . '/edit', $changes, 'Save');
$node1 = node_load($node->nid, $node->vid, TRUE);
$node2 = node_load($node->nid, NULL, TRUE);
$this
->reorderLocations($node, 'field_loctest');
$this
->reorderLocations($node1, 'field_loctest');
$this
->reorderLocations($node2, 'field_loctest');
$this
->assertEqual(count($node1->field_loctest), 2, t('Ensuring second revision did not affect first revision'));
$this
->pass(count($node1->field_loctest));
$this
->assertEqual($node->field_loctest[0]['lid'], $node1->field_loctest[0]['lid'], t('Ensuring second revision did not affect first revision'));
$this
->assertEqual($node->field_loctest[1]['lid'], $node1->field_loctest[1]['lid'], t('Ensuring second revision did not affect first revision'));
$this
->assertEqual(count($node2->field_loctest), 2, t('Ensuring second revision does not have stealth locations'));
db_query("DELETE FROM {node_revisions} WHERE nid = %d AND vid = %d", $node1->nid, $node1->vid);
node_invoke_nodeapi($node1, 'delete revision');
$result = db_query('SELECT * FROM {location} WHERE lid = %d', $node1->field_loctest[0]['lid']);
if ($row = db_fetch_object($result)) {
$this
->fail(t('Ensuring location on deleted revision is garbage collected'));
}
else {
$this
->pass(t('Ensuring location on deleted revision is garbage collected'));
}
$result = db_query('SELECT * FROM {location} WHERE lid = %d', $node1->field_loctest[1]['lid']);
if ($row = db_fetch_object($result)) {
$this
->pass(t('Ensuring shared location on deleted revision is NOT garbage collected'));
}
else {
$this
->fail(t('Ensuring shared location on deleted revision is NOT garbage collected'));
}
}
function testNodeRevisionCleanup() {
$settings = array();
$location_type = $this
->addLocationContentType($settings);
$location1_name = $this
->randomName();
$node = $this
->drupalCreateNode(array(
'type' => $location_type,
'field_loctest' => array(
array(
'name' => $location1_name,
),
),
));
$node = node_load($node->nid, NULL, TRUE);
$changes = array(
'revision' => TRUE,
'log' => $this
->randomName(20),
);
$this
->flattenPostData($changes);
$this
->drupalPost('node/' . $node->nid . '/edit', $changes, 'Save');
$node1 = node_load($node->nid, $node->vid, TRUE);
$node2 = node_load($node->nid, NULL, TRUE);
$this
->deleteNode($node->nid);
$result = db_query('SELECT * FROM {location} WHERE lid = %d', $node1->field_loctest[0]['lid']);
if ($row = db_fetch_object($result)) {
$this
->fail(t('Ensuring all revisions are cleaned up when a multiple revision node is deleted'));
}
else {
$this
->pass(t('Ensuring all revisions are cleaned up when a multiple revision node is deleted'));
}
}
function testCOWConservation() {
$settings = array();
$location_type = $this
->addLocationContentType($settings);
$location1_name = $this
->randomName();
$node = $this
->drupalCreateNode(array(
'type' => $location_type,
'field_loctest' => array(
0 => array(
'name' => $location1_name,
'location_settings' => $settings,
),
),
));
$node = node_load($node->nid, NULL, TRUE);
$changes = array(
'field_loctest' => array(
0 => array(
'name' => $location1_name . '_CHANGE',
),
),
);
$this
->flattenPostData($changes);
$this
->drupalPost('node/' . $node->nid . '/edit', $changes, 'Save');
$node1 = node_load($node->nid, NULL, TRUE);
$this
->reorderLocations($node, 'field_loctest');
$this
->reorderLocations($node1, 'field_loctest');
$this
->assertEqual($node->field_loctest[0]['lid'], $node1->field_loctest[0]['lid'], t('Ensuring LIDs are conserved'));
}
}