View source
<?php
class autocompleteWidgetsBaseTest extends DrupalWebTestCase {
function setUp() {
parent::setUp('autocomplete_widgets');
menu_rebuild();
$this->test_values = array(
'fooBar',
'FooBar',
'foobar',
'foo<em>baz</em>',
'bazfoo',
'baz',
);
$this->admin_user = $this
->drupalCreateUser(array(
'access administration pages',
'bypass node access',
'administer nodes',
));
$this
->drupalLogin($this->admin_user);
$this->settings = array(
'autocomplete_match' => 'contains',
'autocomplete_case' => '0',
'autocomplete_xss' => '0',
'suggested_values' => implode("\n", $this->test_values),
'order' => '',
'allowed_node_types' => array(
'article',
),
);
}
function createInstance($field, $widget_type, $settings) {
$instance = array(
'field_name' => $this->field['field_name'],
'entity_type' => 'node',
'bundle' => 'article',
'widget' => array(
'type' => $widget_type,
'settings' => $settings,
),
);
return field_create_instance($instance);
}
function basicTest($instance) {
$instance['widget']['settings'] = $this->settings;
field_update_instance($instance);
$this
->drupalGet($this->path . 'f');
$this
->assertRaw('"foobar"', 'A suggestion was returned containing the search string.');
$this
->assertRaw('"fooBar"', 'A suggestion was returned containing the search string.');
$this
->assertRaw('"FooBar"', 'A suggestion was returned containing the search string.');
$this
->assertRaw(drupal_json_encode('foo<em>baz</em>'), 'A suggestion was returned containing the search string.');
$this
->assertRaw('"bazfoo"', 'A suggestion was returned containing the search string.');
$this
->assertNoRaw('"baz"', 'Non-matching value was not returned.');
}
function startsWithTest($instance) {
$settings = $this->settings;
$settings['autocomplete_match'] = 'starts_with';
$instance['widget']['settings'] = $settings;
field_update_instance($instance);
$this
->drupalGet($this->path . 'foo');
$this
->assertRaw('"foobar"', 'A suggestion was returned starting with the search string.');
$this
->assertNoRaw('"bazfoo"', 'A suggestion containing but not starting with the search string was not returned.');
}
function caseTest($instance) {
$settings = $this->settings;
$settings['autocomplete_case'] = '1';
$instance['widget']['settings'] = $settings;
field_update_instance($instance);
$this
->drupalGet($this->path . 'F');
$this
->assertRaw('"FooBar"', 'A case-sensitive suggestion was returned containing the search string.');
$this
->assertNoRaw('"fooBar"', 'Non-matching case-sensitve value was not returned.');
}
function xssTest($instance) {
$settings = $this->settings;
$settings['autocomplete_xss'] = 1;
$instance['widget']['settings'] = $settings;
field_update_instance($instance);
$this
->drupalGet($this->path . 'foo');
$this
->assertRaw('"bazfoo"', 'A suggestion was returned with HTML stripped out.');
}
}
class autocompleteWidgetsAllowedValuesTest extends autocompleteWidgetsBaseTest {
public static function getInfo() {
return array(
'name' => t('Allowed Values Widget'),
'description' => t('Tests for the allowed values autocomplete widget.'),
'group' => t('Autocomplete Widgets'),
);
}
function setUp() {
parent::setUp();
$this->field = array(
'field_name' => 'allowed_values',
'type' => 'list_text',
'cardinality' => 1,
'settings' => array(
'allowed_values' => $this->test_values,
),
);
$this->field = field_create_field($this->field);
$this->path = 'autocomplete_widgets/node/article/' . $this->field['field_name'] . '/';
}
function testAllowedValues() {
$instance = $this
->createInstance($this->field, 'autocomplete_widgets_allowvals', $this->settings);
$this
->basicTest($instance);
$this
->startsWithTest($instance);
$this
->caseTest($instance);
$this
->xssTest($instance);
}
}
class autocompleteWidgetsFieldDataTest extends autocompleteWidgetsBaseTest {
public static function getInfo() {
return array(
'name' => t('Existing Field Data Widget'),
'description' => t('Tests for the existing field data autocomplete widget.'),
'group' => t('Autocomplete Widgets'),
);
}
function setUp() {
parent::setUp();
$this->field = array(
'field_name' => 'existing_data',
'type' => 'text',
'cardinality' => 1,
'settings' => array(),
);
$this->field = field_create_field($this->field);
$this->path = 'autocomplete_widgets/node/article/' . $this->field['field_name'] . '/';
}
function testFieldData() {
$instance = $this
->createInstance($this->field, 'autocomplete_widgets_flddata', $this->settings);
foreach ($this->test_values as $value) {
$settings = array(
'type' => 'article',
$this->field['field_name'] => array(
LANGUAGE_NONE => array(
array(
'value' => $value,
),
),
),
);
$node = $this
->drupalCreateNode($settings);
}
$this
->basicTest($instance);
$this
->startsWithTest($instance);
$this
->caseTest($instance);
$this
->xssTest($instance);
}
}
class autocompleteWidgetsSuggestedValuesTest extends autocompleteWidgetsBaseTest {
public static function getInfo() {
return array(
'name' => t('Suggested Values Widget'),
'description' => t('Tests for the suggested values autocomplete widget.'),
'group' => t('Autocomplete Widgets'),
);
}
function setUp() {
parent::setUp();
$this->field = array(
'field_name' => 'suggested_values',
'type' => 'text',
'cardinality' => 1,
'settings' => array(),
);
$this->field = field_create_field($this->field);
$this->path = 'autocomplete_widgets/node/article/' . $this->field['field_name'] . '/';
}
function testSuggestedValues() {
$instance = $this
->createInstance($this->field, 'autocomplete_widgets_suggested', $this->settings);
$this
->basicTest($instance);
$this
->startsWithTest($instance);
$this
->caseTest($instance);
$this
->xssTest($instance);
}
}
class autocompleteWidgetsNodeReferenceTest extends autocompleteWidgetsBaseTest {
public static function getInfo() {
return array(
'name' => t('Node Reference Values Widget'),
'description' => t('Tests for the node reference values autocomplete widget.'),
'group' => t('Autocomplete Widgets'),
);
}
function setUp() {
parent::setUp();
$this->field = array(
'field_name' => 'node_reference_data',
'type' => 'text',
'cardinality' => 1,
'settings' => array(),
);
$this->field = field_create_field($this->field);
$this->path = 'autocomplete_widgets/node/article/' . $this->field['field_name'] . '/';
}
function testNodeTitles() {
$instance = $this
->createInstance($this->field, 'autocomplete_widgets_node_reference', $this->settings);
foreach ($this->test_values as $value) {
$settings = array(
'type' => 'article',
'title' => $value,
);
$node = $this
->drupalCreateNode($settings);
}
$this
->basicTest($instance);
$this
->startsWithTest($instance);
$this
->caseTest($instance);
$this
->xssTest($instance);
}
}