updated link module

This commit is contained in:
Bachir Soussi Chiadmi
2016-04-19 16:40:02 +02:00
parent e2fde76aff
commit aea9683c77
8 changed files with 536 additions and 70 deletions

View File

@@ -254,6 +254,41 @@ class LinkAttributeCrudTest extends DrupalWebTestCase {
}
}
function testFormatterHost() {
$content_type_friendly = $this->randomName(20);
$content_type_machine = strtolower($this->randomName(10));
$this->drupalCreateContentType(array(
'type' => $content_type_machine,
'name' => $content_type_friendly,
));
// Now add a singleton field.
$single_field_name_friendly = $this->randomName(20);
$single_field_name_machine = strtolower($this->randomName(10));
//$single_field_name = 'field_'. $single_field_name_machine;
$this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
// Okay, now we want to make sure this display is changed:
$this->drupalGet('admin/structure/types/manage/'. $content_type_machine .'/display');
$edit = array(
'fields[field_'. $single_field_name_machine .'][label]' => 'above',
'fields[field_'. $single_field_name_machine .'][type]' => 'link_host',
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->createNodeTypeUser($content_type_machine);
$link_text = 'Display';
$link_url = 'http://www.example.com/';
$this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
$this->assertText('www.example.com');
$this->assertNoText($link_text);
$this->assertNoLinkByHref($link_url);
}
function testFormatterURL() {
$content_type_friendly = $this->randomName(20);
$content_type_machine = strtolower($this->randomName(10));

View File

@@ -312,4 +312,148 @@ class LinkUITest extends DrupalWebTestcase {
$this->assertFalse($instance['settings']['attributes']['class'], 'By default, no class should be set.');
$this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
}
/**
* If we're creating a new field and just hit 'save' on the default options, we want to make
* sure they are set to the expected results.
*/
function testCRUDCreateFieldWithClass() {
$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());
$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'));
$link_class_name = 'basic-link-' . strtolower($this->randomName());
$edit = array(
'instance[settings][attributes][class]' => $link_class_name,
);
$this->drupalPost(NULL, $edit, t('Save settings'));
// Is field created?
$this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
node_types_rebuild();
menu_rebuild();
_field_info_collate_fields(TRUE);
$instances = field_info_instances('node', 'page');
$instance = $instances['field_' . $name];
$this->assertFalse($instance['required'], 'Make sure field is not required.');
$this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
$this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');
$this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');
$this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
$this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
$this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
$this->assertEqual($instance['settings']['attributes']['class'], $link_class_name, 'One class should be set.');
$this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
// Now, let's create a node with this field and make sure the link shows up:
// create page form
$field_name = 'field_' . $name;
$this->drupalGet('node/add/page');
$this->assertField($field_name . '[und][0][url]', 'URL found');
$input = array(
'title' => 'This & That',
'href' => 'http://www.example.com/',
);
$edit = array(
'title' => $field_name,
$field_name . '[und][0][title]' => $input['title'],
$field_name . '[und][0][url]' => $input['href'],
);
$this->drupalPost(NULL, $edit, t('Save'));
$url = $this->getUrl();
// change to anonymous user
$this->drupalLogout();
$this->drupalGet($url);
$this->assertRaw('This & That');
$this->assertPattern('|class\s?=\s?"' . $link_class_name . '"|', "Class $link_class_name exists on page.");
}
/**
* If we're creating a new field and just hit 'save' on the default options, we want to make
* sure they are set to the expected results.
*/
function testCRUDCreateFieldWithTwoClasses() {
$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());
$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'));
$link_class_name = 'basic-link ' . strtoupper($this->randomName());
$edit = array(
'instance[settings][attributes][class]' => $link_class_name,
);
$this->drupalPost(NULL, $edit, t('Save settings'));
// Is field created?
$this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
node_types_rebuild();
menu_rebuild();
_field_info_collate_fields(TRUE);
$instances = field_info_instances('node', 'page');
$instance = $instances['field_' . $name];
$this->assertFalse($instance['required'], 'Make sure field is not required.');
$this->assertEqual($instance['settings']['title'], 'optional', 'Title should be optional by default.');
$this->assertTrue($instance['settings']['validate_url'], 'Make sure validation is on.');
$this->assertTrue($instance['settings']['enable_tokens'], 'Enable Tokens should be on by default.');
$this->assertEqual($instance['settings']['display']['url_cutoff'], 80, 'Url cutoff should be at 80 characters.');
$this->assertEqual($instance['settings']['attributes']['target'], 'default', 'Target should be "default"');
$this->assertFalse($instance['settings']['attributes']['rel'], 'Rel should be blank by default.');
$this->assertEqual($instance['settings']['attributes']['class'], $link_class_name, 'Two classes should be set.');
$this->assertFalse($instance['settings']['title_value'], 'By default, no title should be set.');
// Now, let's create a node with this field and make sure the link shows up:
// create page form
$field_name = 'field_' . $name;
$this->drupalGet('node/add/page');
$this->assertField($field_name . '[und][0][url]', 'URL found');
$input = array(
'title' => 'This & That',
'href' => 'http://www.example.com/',
);
$edit = array(
'title' => $field_name,
$field_name . '[und][0][title]' => $input['title'],
$field_name . '[und][0][url]' => $input['href'],
);
$this->drupalPost(NULL, $edit, t('Save'));
$url = $this->getUrl();
// change to anonymous user
$this->drupalLogout();
$this->drupalGet($url);
$this->assertRaw('This & That');
$this->assertPattern('|class\s?=\s?"' . $link_class_name . '"|', "Classes $link_class_name exist on page.");
}
}

View File

@@ -0,0 +1,155 @@
<?php
/**
* @file
* Contains simpletests making sure entity_token integration works.
*/
/**
* Testing that tokens can be used in link titles
*/
class LinkEntityTokenTest extends LinkBaseTestClass {
public static function getInfo() {
return array(
'name' => 'Link entity tokens test',
'description' => 'Tests that a link field appears properly in entity tokens',
'group' => 'Link',
'dependencies' => array('token', 'entity', 'entity_token'),
);
}
function setUp($modules = array()) {
parent::setUp(array('token', 'entity', 'entity_token'));
}
/**
* Creates a link field, fills it, then uses a loaded node to test tokens.
*/
function testFieldTokenNodeLoaded() {
// create field
$settings = array(
'instance[settings][enable_tokens]' => 0,
);
$field_name = $this->createLinkField('page',
$settings);
// create page form
$this->drupalGet('node/add/page');
//$field_name = 'field_' . $name;
$this->assertField($field_name . '[und][0][title]', 'Title found');
$this->assertField($field_name . '[und][0][url]', 'URL found');
$token_url_tests = array(
1 => array(
'href' => 'http://example.com/' . $this->randomName(),
'label' => $this->randomName(),
),
2 => array(
'href' => 'http://example.com/' . $this->randomName() . '?property=value',
'label' => $this->randomName(),
),
3 => array(
'href' => 'http://example.com/' . $this->randomName() . '#position',
'label' => $this->randomName(),
),
4 => array(
'href' => 'http://example.com/' . $this->randomName() . '#lower?property=value2',
'label' => $this->randomName(),
),
);
//$this->assert('pass', '<pre>' . print_r($token_url_tests, TRUE) . '<pre>');
foreach ($token_url_tests as &$input) {
$this->drupalGet('node/add/page');
$edit = array(
'title' => $input['label'],
$field_name . '[und][0][title]' => $input['label'],
$field_name . '[und][0][url]' => $input['href'],
);
$this->drupalPost(NULL, $edit, t('Save'));
$url = $this->getUrl();
$input['url'] = $url;
}
// change to anonymous user
$this->drupalLogout();
foreach ($token_url_tests as $index => $input2) {
$node = node_load($index);
$this->assertNotEqual(NULL, $node, "Do we have a node?");
$this->assertEqual($node->nid, $index, "Test that we have a node.");
$token_name = '[node:' . str_replace('_', '-', $field_name) . ':url]';
$assert_data = token_replace($token_name,
array('node' => $node));
$this->assertEqual($input2['href'], $assert_data, "Test that the url token has been set to " . $input2['href'] . ' - ' . $assert_data);
}
}
/**
* Creates a link field, fills it, then uses a loaded and node_view'd node to test tokens.
*/
function testFieldTokenNodeViewed() {
// create field
$settings = array(
'instance[settings][enable_tokens]' => 0,
);
$field_name = $this->createLinkField('page',
$settings);
// create page form
$this->drupalGet('node/add/page');
//$field_name = 'field_' . $name;
$this->assertField($field_name . '[und][0][title]', 'Title found');
$this->assertField($field_name . '[und][0][url]', 'URL found');
$token_url_tests = array(
1 => array(
'href' => 'http://example.com/' . $this->randomName(),
'label' => $this->randomName(),
),
2 => array(
'href' => 'http://example.com/' . $this->randomName() . '?property=value',
'label' => $this->randomName(),
),
3 => array(
'href' => 'http://example.com/' . $this->randomName() . '#position',
'label' => $this->randomName(),
),
4 => array(
'href' => 'http://example.com/' . $this->randomName() . '#lower?property=value2',
'label' => $this->randomName(),
),
);
//$this->assert('pass', '<pre>' . print_r($token_url_tests, TRUE) . '<pre>');
foreach ($token_url_tests as &$input) {
$this->drupalGet('node/add/page');
$edit = array(
'title' => $input['label'],
$field_name . '[und][0][title]' => $input['label'],
$field_name . '[und][0][url]' => $input['href'],
);
$this->drupalPost(NULL, $edit, t('Save'));
$url = $this->getUrl();
$input['url'] = $url;
}
// change to anonymous user
$this->drupalLogout();
foreach ($token_url_tests as $index => $input2) {
$node = node_load($index);
$node_array = node_view($node, 'full');
$this->assertNotEqual(NULL, $node, "Do we have a node?");
$this->assertEqual($node->nid, $index, "Test that we have a node.");
$token_name = '[node:' . str_replace('_', '-', $field_name) . ':url]';
$assert_data = token_replace($token_name,
array('node' => $node));
$this->assertEqual($input2['href'], $assert_data, "Test that the url token has been set to " . $input2['href'] . ' - ' . $assert_data);
}
}
}

View File

@@ -426,4 +426,6 @@ class LinkTokenTest extends LinkBaseTestClass {
$this->assertRaw('This &amp; That');
}
}

View File

@@ -22,25 +22,24 @@ class LinkValidateTestCase extends LinkBaseTestClass {
$field_name = $this->createLinkField();
$permission = 'create page content';
$this->checkPermissions(array($permission), TRUE);
$this->drupalGet('node/add/page');
$label = $this->randomName();
$edit = array(
$settings = array(
'title' => $label,
$field_name . '[und][0][title]' => $label,
$field_name . '[und][0][url]' => $url,
$field_name => array(
LANGUAGE_NONE=> array(
array(
'title' => $label,
'url' => $url,
)
),
),
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertRaw(' has been created.', 'Node created');
$nid = 1; //$matches[1];
$node = $this->drupalCreateNode($settings);
$node = node_load($nid);
$this->assertNotNull($node, ' has been created.', 'Node created');
$this->assertEqual($url, $node->{$field_name}['und'][0]['url']);
$this->assertEqual($url, $node->{$field_name}[LANGUAGE_NONE][0]['url']);
}
}
@@ -269,7 +268,13 @@ class LinkValidateTest extends LinkValidateTestCase {
// Validate that an internal url would be accepted.
function test_link_internal_url() {
$this->link_test_validate_url('node/32');
// Create the content first.
$node = $this->drupalCreateNode();
$link = 'node/' . $node->nid;
$this->link_test_validate_url($link);
$type = link_url_type($link);
$this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
}
// Validate a simple mailto.
@@ -340,7 +345,7 @@ class LinkValidateSpecificURL extends LinkValidateTestCase {
/**
* Here, we're testing that a very long url is stored properly in the db.
*
* Basicly, trying to test http://drupal.org/node/376818
* Basically, trying to test http://drupal.org/node/376818
*/
function testLinkURLFieldIsBig() {
$long_url = 'http://th.wikipedia.org/wiki/%E0%B9%82%E0%B8%A3%E0%B8%87%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B9%80%E0%B8%9A%E0%B8%8D%E0%B8%88%E0%B8%A1%E0%B8%A3%E0%B8%B2%E0%B8%8A%E0%B8%B9%E0%B8%97%E0%B8%B4%E0%B8%A8_%E0%B8%99%E0%B8%84%E0%B8%A3%E0%B8%A8%E0%B8%A3%E0%B8%B5%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%A3%E0%B8%B2%E0%B8%8A';
@@ -363,6 +368,10 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
'group' => 'Link',
);
}
function setUp() {
parent::setUp('link');
}
/**
* Translates the LINK type constants to english for display and debugging of tests
@@ -386,10 +395,10 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
}
}
// Make sure that a link labelled <front> works.
// Make sure that a link labeled <front> works.
function testValidateFrontLink() {
$valid = link_validate_url('<front>');
$this->assertEqual(LINK_FRONT, $valid, 'Make sure that front link is verfied and identified');
$this->assertEqual(LINK_FRONT, $valid, 'Make sure that front link is verified and identified');
}
function testValidateEmailLink() {
@@ -409,7 +418,7 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
function testValidateNewsArticleLink() {
$valid = link_validate_url('news:hj0db8$vrm$1@news.eternal-september.org');
$this->assertEqual(LINK_NEWS, $valid, 'Make sure link to specific article valiates as news.');
$this->assertEqual(LINK_NEWS, $valid, 'Make sure link to specific article validates as news.');
}
function testValidateBadNewsgroupLink() {
@@ -418,16 +427,18 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
}
function testValidateInternalLinks() {
$tempfile = drupal_tempnam('public://files', 'test');
$links = array(
'node/5',
'rss.xml',
'files/test.jpg',
'/var/www/test',
file_uri_target($tempfile),
drupal_realpath($tempfile),
);
foreach ($links as $link) {
$type = link_url_type($link);
$this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
$valid = link_validate_url($link);
$this->assertEqual(LINK_INTERNAL, $valid, 'Test ' . $link . ' internal link.');
$this->assertTrue($valid, 'Test ' . $link . ' is valid internal link.');
}
}
@@ -446,7 +457,6 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
'http://255.255.255.255:4823/',
'www.test-site.com',
'http://example.com/index.php?q=node/123',
'http://example.com/index.php?page=this\that',
'http://example.com/?first_name=Joe Bob&last_name=Smith',
// Anchors
'http://www.example.com/index.php#test',
@@ -464,8 +474,10 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
}
}
foreach ($links as $link) {
$type = link_url_type($link);
$this->assertEqual(LINK_EXTERNAL, $type, 'Testing that ' . $link . ' is an external link.');
$valid = link_validate_url($link);
$this->assertEqual(LINK_EXTERNAL, $valid, 'Testing that ' . $link . ' is a valid external link.');
$this->assertTrue($valid, 'Test ' . $link . ' is valid external link.');
// The following two lines are commented out and only used for comparisons.
//$valid2 = valid_url($link, TRUE);
//$this->assertEqual(TRUE, $valid2, "Using valid_url() on $link.");
@@ -485,6 +497,8 @@ class LinkValidateUrlLight extends DrupalWebTestCase {
'http://www.testß.com/', // ß not allowed in domain names!
'http://www.example.frog/', // Bad TLD
//'http://www.-fudge.com/', // domains can't have sections starting with a dash.
'http://example.com/index.php?page=this\that',
'example@example.com',
);
foreach ($links as $link) {
$valid = link_validate_url($link);