Commit 3df6e1cb authored by John Fiala's avatar John Fiala
Browse files

Issue #1674284 by sun, jcfiala: Cleaning up the code to Drupal standards, this time with the tests.

parent 537d70db
......@@ -6,10 +6,9 @@
*/
class LinkAttributeCrudTest extends DrupalWebTestCase {
private $zebra;
public $permissions = array(
protected $permissions = array(
'access content',
'administer content types',
'administer nodes',
......@@ -29,14 +28,14 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
}
function setup() {
parent::setup('field_ui', 'link');
$this->zebra = 0;
parent::setup('field_ui', 'link'); // was 'views'
// Create and login user.
$account = $this->drupalCreateUser(array('administer content types'));
$this->drupalLogin($account);
$this->web_user = $this->drupalCreateUser(array('administer content types'));
$this->drupalLogin($this->web_user);
}
function createLink($url, $title, $attributes = array()) {
protected function createLink($url, $title, $attributes = array()) {
return array(
'url' => $url,
'title' => $title,
......@@ -44,7 +43,7 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
);
}
private function assertLinkOnNode($field_name, $link_value, $message = '', $group = 'Other') {
protected function assertLinkOnNode($field_name, $link_value, $message = '', $group = 'Other') {
$this->zebra++;
$zebra_string = ($this->zebra % 2 == 0) ? 'even' : 'odd';
$cssFieldLocator = 'field-'. str_replace('_', '-', $field_name);
......@@ -109,8 +108,8 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
// Now that we have a new content type, create a user that has privileges
// on the content type.
$permissions = array_merge($this->permissions, array($permission));
$account = $this->drupalCreateUser($permissions);
$this->drupalLogin($account);
$this->web_user = $this->drupalCreateUser($permissions);
$this->drupalLogin($this->web_user);
// Go to page.
$this->drupalGet('node/add/'. $content_type_machine);
......@@ -127,7 +126,7 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
$this->drupalGet('node/add/'. $content_type_machine);
// lets add a node:
// Create a node:
$edit = array(
'title' => $title,
'field_' . $single_field_name_machine . '[und][0][url]' => 'http://www.example.com/',
......@@ -142,21 +141,8 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
$this->assertLinkByHref('http://www.example.com');
}
private function createNodeType($content_type_machine, $content_type_friendly) {
$this->drupalGet('admin/structure/types');
// Create the content type.
$this->clickLink(t('Add content type'));
$edit = array (
'name' => $content_type_friendly,
'type' => $content_type_machine,
);
$this->drupalPost(NULL, $edit, t('Save and add fields'));
$this->assertText(t('The content type @name has been added.', array('@name' => $content_type_friendly)));
}
private function createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine) {
protected function createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine) {
$this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/fields');
$edit = array (
'fields[_add_new_field][label]' => $single_field_name_friendly,
'fields[_add_new_field][field_name]' => $single_field_name_machine,
......@@ -180,7 +166,7 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
$this->assertTrue($type_exists, 'The new content type has been created in the database.');
}
function createNodeTypeUser($content_type_machine) {
protected function createNodeTypeUser($content_type_machine) {
$permission = 'create ' . $content_type_machine . ' content';
$permission_edit = 'edit ' . $content_type_machine . ' content';
// Reset the permissions cache.
......@@ -189,12 +175,11 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
// Now that we have a new content type, create a user that has privileges
// on the content type.
$permissions = array_merge($this->permissions, array($permission));
$account = $this->drupalCreateUser($permissions);
$this->drupalLogin($account);
$this->web_user = $this->drupalCreateUser($permissions);
$this->drupalLogin($this->web_user);
}
function createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $title, $url, $node_title = '') {
// Go to page.
protected function createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $title, $url, $node_title = '') {
$this->drupalGet('node/add/'. $content_type_machine);
if (!$node_title) {
......@@ -222,7 +207,10 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
$content_type_friendly = $this->randomName(20);
$content_type_machine = strtolower($this->randomName(10));
$this->createNodeType($content_type_machine, $content_type_friendly);
$this->drupalCreateContentType(array(
'type' => $content_type_machine,
'name' => $content_type_friendly,
));
// Now add a singleton field.
$single_field_name_friendly = $this->randomName(20);
......@@ -270,7 +258,10 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
$content_type_friendly = $this->randomName(20);
$content_type_machine = strtolower($this->randomName(10));
$this->createNodeType($content_type_machine, $content_type_friendly);
$this->drupalCreateContentType(array(
'type' => $content_type_machine,
'name' => $content_type_friendly,
));
// Now add a singleton field.
$single_field_name_friendly = $this->randomName(20);
......@@ -317,7 +308,10 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
$content_type_friendly = $this->randomName(20);
$content_type_machine = strtolower($this->randomName(10));
$this->createNodeType($content_type_machine, $content_type_friendly);
$this->drupalCreateContentType(array(
'type' => $content_type_machine,
'name' => $content_type_friendly,
));
// Now add a singleton field.
$single_field_name_friendly = $this->randomName(20);
......@@ -365,7 +359,10 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
$content_type_friendly = $this->randomName(20);
$content_type_machine = strtolower($this->randomName(10));
$this->createNodeType($content_type_machine, $content_type_friendly);
$this->drupalCreateContentType(array(
'type' => $content_type_machine,
'name' => $content_type_friendly,
));
// Now add a singleton field.
$single_field_name_friendly = $this->randomName(20);
......@@ -413,7 +410,10 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
$content_type_friendly = $this->randomName(20);
$content_type_machine = strtolower($this->randomName(10));
$this->createNodeType($content_type_machine, $content_type_friendly);
$this->drupalCreateContentType(array(
'type' => $content_type_machine,
'name' => $content_type_friendly,
));
// Now add a singleton field.
$single_field_name_friendly = $this->randomName(20);
......@@ -462,7 +462,10 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
$content_type_friendly = $this->randomName(20);
$content_type_machine = strtolower($this->randomName(10));
$this->createNodeType($content_type_machine, $content_type_friendly);
$this->drupalCreateContentType(array(
'type' => $content_type_machine,
'name' => $content_type_friendly,
));
// Now add a singleton field.
$single_field_name_friendly = $this->randomName(20);
......@@ -488,45 +491,4 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
$this->assertNoText($link_url);
$this->assertNoLinkByHref($link_url);
}
/**
* This test sees that we can create a link field with a defined class, and make sure
* that class displays properly when the link is displayed.
*/
/*function testLinkWithClassOnField() {
$this->acquireContentTypes(1);
$field_settings = array(
'type' => 'link',
'widget_type' => 'link',
'type_name' => $this->content_types[0]->name,
'attributes' => array(
'class' => 'test-class',
'target' => 'default',
'rel' => FALSE,
),
);
$field = $this->createField($field_settings, 0);
//$this->pass('<pre>'. print_r($field, TRUE) .'</pre>');
$field_db_info = content_database_info($field);
$this->acquireNodes(2);
$node = node_load($this->nodes[0]->nid);
$node->promote = 1; // We want this to show on front page for the teaser test.
$node->{$field['field_name']}[0] = $this->createLink('http://www.example.com', 'Test Link');
node_save($node);
// Does this display on the node page?
$this->drupalGet('node/'. $this->nodes[0]->nid);
//$this->outputScreenContents('Link field with class', 'link_');
$this->assertLinkOnNode($field['field_name'], l('Test Link', 'http://www.example.com', array('attributes' => array('class' => 'test-class'))));
// Does this display on the front page?
$this->drupalGet('<front>');
// reset the zebra!
$this->zebra = 0;
$this->assertLinkOnNode($field['field_name'], l('Test Link', 'http://www.example.com', array('attributes' => array('class' => 'test-class'))));
}*/
}
......@@ -16,8 +16,7 @@ class LinkContentCrudTest extends DrupalWebTestCase {
}
function setUp() {
parent::setUp('field_ui', 'link'); // was views?
//$this->loginWithPermissions();
parent::setUp('field_ui', 'link');
}
/**
......@@ -30,15 +29,14 @@ class LinkContentCrudTest extends DrupalWebTestCase {
$title = $this->randomName(20);
// Create and login user.
$account = $this->drupalCreateUser(array('administer content types'));
$this->drupalLogin($account);
$this->web_user = $this->drupalCreateUser(array('administer content types'));
$this->drupalLogin($this->web_user);
$this->drupalGet('admin/structure/types');
// Create the content type.
$this->clickLink(t('Add content type'));
$edit = array (
'name' => $content_type_friendly,
'type' => $content_type_machine,
......@@ -55,7 +53,6 @@ class LinkContentCrudTest extends DrupalWebTestCase {
'fields[_add_new_field][field_name]' => $single_field_name_machine,
'fields[_add_new_field][type]' => 'link_field',
'fields[_add_new_field][widget_type]' => 'link_field',
);
$this->drupalPost(NULL, $edit, t('Save'));
......
......@@ -42,13 +42,15 @@ class LinkUITest extends DrupalWebTestcase {
*/
function testLinkCreate() {
//libxml_use_internal_errors(true);
$account = $this->drupalCreateUser(array('administer content types',
'administer nodes',
'administer filters',
'access content',
'create page content',
'access administration pages'));
$this->drupalLogin($account);
$this->web_user = $this->drupalCreateUser(array(
'administer content types',
'administer nodes',
'administer filters',
'access content',
'create page content',
'access administration pages'
));
$this->drupalLogin($this->web_user);
// create field
$name = strtolower($this->randomName());
......@@ -125,7 +127,7 @@ class LinkUITest extends DrupalWebTestcase {
$input_test_cases[] = $test_case;
foreach ($input_test_cases as $input) {
$this->drupalLogin($account);
$this->drupalLogin($this->web_user);
$this->drupalGet('node/add/page');
$edit = array(
......@@ -167,122 +169,13 @@ class LinkUITest extends DrupalWebTestcase {
//libxml_use_internal_errors(FALSE);
}
/**
* Creates a link field for the "page" type and creates a page with a link.
* Just like the above test, only here we're turning off the validation on the field.
*/
/*function testLinkCreate_NoValidation() {
//libxml_use_internal_errors(true);
$account = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
$this->drupalLogin($account);
// create field
$name = strtolower($this->randomName());
$edit = array(
'fields[_add_new_field][label]' => $name,
'fields[_add_new_field][field_name]' => $name,
'fields[_add_new_field][type]' => 'link_field',
'fields[_add_new_field][widget_type]' => 'link_field',
);
$this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
$this->drupalPost(NULL, array(), t('Save field settings'));
$this->drupalPost(NULL, array('validate_url' => FALSE), t('Save settings'));
// Is field created?
$this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
_content_type_info(TRUE);
$fields = content_fields();
$this->assertTRUE(0 === $fields['field_'. $name]['validate_url'], 'Make sure validation is off.');
// create page form
$this->drupalGet('node/add/page');
$field_name = 'field_' . $name;
$this->assertField($field_name . '[0][title]', 'Title found');
$this->assertField($field_name . '[0][url]', 'URL found');
$input_test_cases = array(
array(
'href' => 'http://example.com/' . $this->randomName(),
'label' => $this->randomName(),
'msg' => 'Link found',
'type' => self::LINK_INPUT_TYPE_GOOD
),
array(
'href' => 'http://example.com/' . $this->randomName(),
'label' => $this->randomName() . '<script>alert("hi");</script>',
'msg' => 'js label',
'type' => self::LINK_INPUT_TYPE_BAD_TITLE
),
array(
'href' => 'http://example.com/' . $this->randomName(),
'label' => $this->randomName() . '<script src="http://devil.site.com"></script>',
'msg' => 'js label',
'type' => self::LINK_INPUT_TYPE_BAD_TITLE
),
array(
'href' => 'http://example.com/' . $this->randomName(),
'label' => $this->randomName() . '" onmouseover="alert(\'hi\')',
'msg' => 'js label',
'type' => self::LINK_INPUT_TYPE_BAD_TITLE
),
array(
'href' => 'http://example.com/' . $this->randomName(),
'label' => $this->randomName() . '\' onmouseover="alert(\'hi\')',
'msg' => 'js label',
'type' => self::LINK_INPUT_TYPE_BAD_TITLE
),
array(
'href' => 'javascript:alert("http://example.com/' . $this->randomName() . '")',
'label' => $this->randomName(),
'msg' => 'js url',
'type' => self::LINK_INPUT_TYPE_BAD_URL
),
);
foreach ($input_test_cases as $input) {
$this->drupalLogin($account);
$this->drupalGet('node/add/page');
$edit = array(
'title' => $input['label'],
$field_name . '[0][title]' => $input['label'],
$field_name . '[0][url]' => $input['href'],
);
$this->drupalPost(NULL, $edit, t('Save'));
if ($input['type'] == self::LINK_INPUT_TYPE_BAD_URL) {
//$this->assertRaw(t('Not a valid URL.'), 'Not a valid URL: ' . $input['href']);
$this->assertNoRaw($input['href']);
$this->assertRaw(t('@type %title has been created.', array('@type' => 'Basic Page', '%title' => $edit['title'])), 'Page created: ' . $input['href']);
continue;
}
else {
$this->assertRaw(t('@type %title has been created.', array('@type' => 'Basic Page', '%title' => $edit['title'])), 'Page created: ' . $input['href']);
}
$url = $this->getUrl();
// change to anonym user
$this->drupalLogout();
$this->drupalGet($url);
//debug($this);
// If simpletest starts using something to override the error system, this will flag
// us and let us know it's broken.
$this->assertFalse(libxml_use_internal_errors(TRUE));
$path = '//a[@href="'. $input['href'] .'" and text()="'. $input['label'] .'"]';
//$this->pass(htmlentities($path));
$elements = $this->xpath($path);
libxml_use_internal_errors(FALSE);
$this->assertIdentical(isset($elements[0]), $input['type'] == self::LINK_INPUT_TYPE_GOOD, $input['msg']);
}
//libxml_use_internal_errors(FALSE);
}*/
/**
* Testing that if you use <strong> in a static title for your link, that the
* title actually displays <strong>.
*/
function testStaticLinkCreate() {
$account = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
$this->drupalLogin($account);
$this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
$this->drupalLogin($this->web_user);
// create field
$name = strtolower($this->randomName());
......@@ -330,8 +223,8 @@ class LinkUITest extends DrupalWebTestcase {
* sure they are set to the expected results.
*/
function testCRUDCreateFieldDefaults() {
$account = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
$this->drupalLogin($account);
$this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
$this->drupalLogin($this->web_user);
// create field
$name = strtolower($this->randomName());
......
......@@ -6,7 +6,7 @@
*/
class LinkBaseTestClass extends DrupalWebTestCase {
public $permissions = array(
protected $permissions = array(
'access content',
'administer content types',
'administer nodes',
......@@ -17,21 +17,18 @@ class LinkBaseTestClass extends DrupalWebTestCase {
'create page content',
);
public $account;
function setUp($modules = array()) {
if ($modules) {
parent::setUp($modules);
}
else {
parent::setUp('field_ui', 'link');
}
$this->account = $this->drupalCreateUser($this->permissions);
$this->drupalLogin($this->account);
function setUp() {
$modules = func_get_args();
$modules = (isset($modules[0]) && is_array($modules[0]) ? $modules[0] : $modules);
$modules = 'field_ui';
$modules = 'link';
parent::setUp($modules);
$this->web_user = $this->drupalCreateUser($this->permissions);
$this->drupalLogin($this->web_user);
}
function createLinkField($node_type = 'page',
$settings = array()) {
protected function createLinkField($node_type = 'page', $settings = array()) {
$name = strtolower($this->randomName());
$edit = array(
'fields[_add_new_field][label]' => $name,
......
......@@ -20,10 +20,7 @@ class LinkTokenTest extends LinkBaseTestClass {
}
function setUp($modules = array()) {
$modules[] = 'field_ui';
$modules[] = 'link';
$modules[] = 'token';
parent::setUp($modules);
parent::setUp(array('token'));
}
/**
......@@ -31,8 +28,8 @@ class LinkTokenTest extends LinkBaseTestClass {
* Creates a node with a token in the link title and checks the value.
*/
function testUserTokenLinkCreate() {
/*$account = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
$this->drupalLogin($account);*/
/*$this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
$this->drupalLogin($this->web_user);*/
// create field
$settings = array(
......@@ -52,7 +49,7 @@ class LinkTokenTest extends LinkBaseTestClass {
'label' => $this->randomName(),
);
//$this->drupalLogin($account);
//$this->drupalLogin($this->web_user);
$this->drupalGet('node/add/page');
$edit = array(
......@@ -92,7 +89,7 @@ class LinkTokenTest extends LinkBaseTestClass {
'href' => 'http://example.com/' . $this->randomName()
);
//$this->drupalLogin($account);
//$this->drupalLogin($this->web_user);
$this->drupalGet('node/add/page');
$edit = array(
......@@ -135,7 +132,7 @@ class LinkTokenTest extends LinkBaseTestClass {
'href' => 'http://example.com/' . $this->randomName()
);
//$this->drupalLogin($account);
//$this->drupalLogin($this->web_user);
$this->drupalGet('node/add/page');
$edit = array(
......@@ -280,8 +277,8 @@ class LinkTokenTest extends LinkBaseTestClass {
* Trying to set the url to contain a token.
*/
function _testUserTokenLinkCreateInURL() {
$account = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
$this->drupalLogin($account);
$this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
$this->drupalLogin($this->web_user);
// create field
$name = strtolower($this->randomName());
......@@ -310,7 +307,7 @@ class LinkTokenTest extends LinkBaseTestClass {
'label' => $this->randomName(),
);
$this->drupalLogin($account);
$this->drupalLogin($this->web_user);
$this->drupalGet('node/add/page');
$edit = array(
......@@ -333,8 +330,8 @@ class LinkTokenTest extends LinkBaseTestClass {
* Trying to set the url to contain a token.
*/
function _testUserTokenLinkCreateInURL2() {
$account = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
$this->drupalLogin($account);
$this->web_user = $this->drupalCreateUser(array('administer content types', 'access content', 'create page content'));
$this->drupalLogin($this->web_user);
// create field
$name = strtolower($this->randomName());
......@@ -363,7 +360,7 @@ class LinkTokenTest extends LinkBaseTestClass {
'label' => $this->randomName(),
);
$this->drupalLogin($account);
$this->drupalLogin($this->web_user);
$this->drupalGet('node/add/page');
$edit = array(
......@@ -378,6 +375,6 @@ class LinkTokenTest extends LinkBaseTestClass {
$this->drupalLogout();
$this->drupalGet($url);
$this->assertRaw(l($input['label'], $input['href'] .'/'. $account->uid));
$this->assertRaw(l($input['label'], $input['href'] .'/'. $this->web_user->uid));
}
}
......@@ -7,11 +7,7 @@
class LinkValidateTestCase extends LinkBaseTestClass {
function setUp($modules = array()) {
parent::setUp($modules);
}
function createLink($url, $title, $attributes = array()) {
protected function createLink($url, $title, $attributes = array()) {
return array(
'url' => $url,
'title' => $title,
......@@ -22,7 +18,7 @@ class LinkValidateTestCase extends LinkBaseTestClass {
/**
* Takes a url, and sees if it can validate that the url is valid.
*/
public function link_test_validate_url($url) {
protected function link_test_validate_url($url) {
$field_name = $this->createLinkField();
......@@ -66,13 +62,13 @@ class LinkValidateTest extends LinkValidateTestCase {
* Test if we're stopped from posting a bad url on default validation.
*/
function test_link_validate_bad_url_validate_default() {
$account = $this->drupalCreateUser(array('administer content types',
$this->web_user = $this->drupalCreateUser(array('administer content types',
'administer nodes',
'administer filters',
'access content',
'create page content',
'access administration pages'));
$this->drupalLogin($account);
$this->drupalLogin($this->web_user);
// create field
$name = strtolower($this->randomName());
......@@ -111,13 +107,13 @@ class LinkValidateTest extends LinkValidateTestCase {
* Test if we're stopped from posting a bad url with validation on.
*/
function test_link_validate_bad_url_validate_on() {
$account = $this->drupalCreateUser(array('administer content types',
$this->web_user = $this->drupalCreateUser(array('administer content types',
'administer nodes',
'administer filters',
'access content',
'create page content',
'access administration pages'));
$this->drupalLogin($account);
$this->drupalLogin($this->web_user);
// create field
$name = strtolower($this->randomName());
......@@ -157,13 +153,13 @@ class LinkValidateTest extends LinkValidateTestCase {
* Test if we can post a bad url if the validation is expressly turned off.
*/
function test_link_validate_bad_url_validate_off() {
$account = $this->drupalCreateUser(array('administer content types',
$this->web_user = $this->drupalCreateUser(array('administer content types',
'administer nodes',
'administer filters',
'access content',
'create page content',
'access administration pages'));
$this->drupalLogin($account);
$this->drupalLogin($this->web_user);
// create field
$name = strtolower($this->randomName());
......@@ -207,13 +203,13 @@ class LinkValidateTest extends LinkValidateTestCase {
*/
function x_test_link_validate_switching_between_validation_status() {
$this->acquireContentTypes(1);
$account = $this->drupalCreateUser(array('administer content types',
$this->web_user = $this->drupalCreateUser(array('administer content types',
'administer nodes',
'access administration pages',