link.attribute.test 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. /**
  3. * @file
  4. * Basic simpletests to test options on link module.
  5. */
  6. class LinkAttributeCrudTest extends DrupalWebTestCase {
  7. private $zebra;
  8. protected $permissions = array(
  9. 'access content',
  10. 'administer content types',
  11. 'administer nodes',
  12. 'administer filters',
  13. 'access comments',
  14. 'post comments',
  15. 'skip comment approval',
  16. 'access administration pages',
  17. );
  18. public static function getInfo() {
  19. return array(
  20. 'name' => 'Link Attribute Tests',
  21. 'description' => 'Tests the field attributes, making sure they appear in various displayed situations.',
  22. 'group' => 'Link',
  23. );
  24. }
  25. function setup() {
  26. parent::setup('field_ui', 'link');
  27. $this->zebra = 0;
  28. // Create and login user.
  29. $perms = user_role_permissions(array(array_search('administrator', user_roles()) => 'administrator'));
  30. $perms = array_keys($perms[array_search('administrator', user_roles())]);
  31. $admin = $this->drupalCreateUser($perms);
  32. $this->drupalLogin($admin);
  33. }
  34. protected function createLink($url, $title, $attributes = array()) {
  35. return array(
  36. 'url' => $url,
  37. 'title' => $title,
  38. 'attributes' => $attributes,
  39. );
  40. }
  41. protected function assertLinkOnNode($field_name, $link_value, $message = '', $group = 'Other') {
  42. $this->zebra++;
  43. $zebra_string = ($this->zebra % 2 == 0) ? 'even' : 'odd';
  44. $cssFieldLocator = 'field-' . str_replace('_', '-', $field_name);
  45. $this->assertPattern('@<div class="field field-type-link ' . $cssFieldLocator . '".*<div class="field-item ' . $zebra_string . '">\s*' . $link_value . '\s*</div>@is',
  46. $message,
  47. $group);
  48. }
  49. /**
  50. * A simple test that just creates a new node type, adds a link field to it, creates a new node of that type, and makes sure
  51. * that the node is being displayed.
  52. */
  53. function testBasic() {
  54. $content_type_friendly = $this->randomName(20);
  55. $content_type_machine = strtolower($this->randomName(10));
  56. $title = $this->randomName(20);
  57. $this->drupalGet('admin/structure/types');
  58. // Create the content type.
  59. $this->clickLink(t('Add content type'));
  60. $edit = array(
  61. 'name' => $content_type_friendly,
  62. 'type' => $content_type_machine,
  63. );
  64. $this->drupalPost(NULL, $edit, t('Save and add fields'));
  65. $this->assertText(t('The content type @name has been added.', array('@name' => $content_type_friendly)));
  66. // Now add a singleton field.
  67. $single_field_name_friendly = $this->randomName(20);
  68. $single_field_name_machine = strtolower($this->randomName(10));
  69. $single_field_name = 'field_' . $single_field_name_machine;
  70. $edit = array(
  71. 'fields[_add_new_field][label]' => $single_field_name_friendly,
  72. 'fields[_add_new_field][field_name]' => $single_field_name_machine,
  73. 'fields[_add_new_field][type]' => 'link_field',
  74. 'fields[_add_new_field][widget_type]' => 'link_field',
  75. );
  76. $this->drupalPost(NULL, $edit, t('Save'));
  77. // We'll go with the default settings for this run-through.
  78. $this->drupalPost(NULL, array(), t('Save field settings'));
  79. // Using all the default settings, so press the button.
  80. $this->drupalPost(NULL, array(), t('Save settings'));
  81. $this->assertText(t('Saved @name configuration.', array('@name' => $single_field_name_friendly)));
  82. // Somehow clicking "save" isn't enough, and we have to do a
  83. // node_types_rebuild().
  84. node_types_rebuild();
  85. menu_rebuild();
  86. $type_exists = db_query('SELECT 1 FROM {node_type} WHERE type = :type', array(':type' => $content_type_machine))->fetchField();
  87. $this->assertTrue($type_exists, 'The new content type has been created in the database.');
  88. $permission = 'create ' . $content_type_machine . ' content';
  89. $permission_edit = 'edit ' . $content_type_machine . ' content';
  90. // Reset the permissions cache.
  91. $this->checkPermissions(array($permission), TRUE);
  92. // Now that we have a new content type, create a user that has privileges
  93. // on the content type.
  94. $permissions = array_merge($this->permissions, array($permission));
  95. $this->web_user = $this->drupalCreateUser($permissions);
  96. $this->drupalLogin($this->web_user);
  97. // Go to page.
  98. $this->drupalGet('node/add/' . $content_type_machine);
  99. // Add a node.
  100. $edit = array(
  101. 'title' => $title,
  102. 'field_' . $single_field_name_machine . '[und][0][title]' => 'Link',
  103. 'field_' . $single_field_name_machine . '[und][0][url]' => 'http://www.drupal.org/',
  104. );
  105. $this->drupalPost(NULL, $edit, t('Save'));
  106. $this->assertText(t('@content_type_friendly @title has been created', array('@content_type_friendly' => $content_type_friendly, '@title' => $title)));
  107. $this->drupalGet('node/add/' . $content_type_machine);
  108. // Create a node:
  109. $edit = array(
  110. 'title' => $title,
  111. 'field_' . $single_field_name_machine . '[und][0][url]' => 'http://www.example.com/',
  112. 'field_' . $single_field_name_machine . '[und][0][title]' => 'Display',
  113. );
  114. // Now we can fill in the second item in the multivalue field and save.
  115. $this->drupalPost(NULL, $edit, t('Save'));
  116. $this->assertText(t('@content_type_friendly @title has been created', array('@content_type_friendly' => $content_type_friendly, '@title' => $title)));
  117. $this->assertText('Display');
  118. $this->assertLinkByHref('http://www.example.com');
  119. }
  120. protected function createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine) {
  121. $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/fields');
  122. $edit = array(
  123. 'fields[_add_new_field][label]' => $single_field_name_friendly,
  124. 'fields[_add_new_field][field_name]' => $single_field_name_machine,
  125. 'fields[_add_new_field][type]' => 'link_field',
  126. 'fields[_add_new_field][widget_type]' => 'link_field',
  127. );
  128. $this->drupalPost(NULL, $edit, t('Save'));
  129. // We'll go with the default settings for this run-through.
  130. $this->drupalPost(NULL, array(), t('Save field settings'));
  131. // Using all the default settings, so press the button.
  132. $this->drupalPost(NULL, array(), t('Save settings'));
  133. $this->assertText(t('Saved @name configuration.', array('@name' => $single_field_name_friendly)));
  134. // Somehow clicking "save" isn't enough, and we have to do a
  135. // node_types_rebuild().
  136. node_types_rebuild();
  137. menu_rebuild();
  138. $type_exists = db_query('SELECT 1 FROM {node_type} WHERE type = :type', array(':type' => $content_type_machine))->fetchField();
  139. $this->assertTrue($type_exists, 'The new content type has been created in the database.');
  140. }
  141. protected function createNodeTypeUser($content_type_machine) {
  142. $permission = 'create ' . $content_type_machine . ' content';
  143. $permission_edit = 'edit ' . $content_type_machine . ' content';
  144. // Reset the permissions cache.
  145. $this->checkPermissions(array($permission), TRUE);
  146. // Now that we have a new content type, create a user that has privileges
  147. // on the content type.
  148. $permissions = array_merge($this->permissions, array($permission));
  149. $this->web_user = $this->drupalCreateUser($permissions);
  150. $this->drupalLogin($this->web_user);
  151. }
  152. protected function createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $title, $url, $node_title = '') {
  153. $this->drupalGet('node/add/' . $content_type_machine);
  154. if (!$node_title) {
  155. $node_title = $this->randomName(20);
  156. }
  157. $edit = array(
  158. 'title' => $node_title,
  159. );
  160. if ($url) {
  161. $edit['field_' . $single_field_name_machine . '[und][0][url]'] = $url;
  162. }
  163. if ($title) {
  164. $edit['field_' . $single_field_name_machine . '[und][0][title]'] = $title;
  165. }
  166. $this->drupalPost(NULL, $edit, t('Save'));
  167. $this->assertText(t('@content_type_friendly @title has been created', array('@content_type_friendly' => $content_type_friendly, '@title' => $node_title)));
  168. }
  169. /**
  170. * Test the link_plain formatter and it's output.
  171. */
  172. function testFormatterPlain() {
  173. $content_type_friendly = $this->randomName(20);
  174. $content_type_machine = strtolower($this->randomName(10));
  175. $this->drupalCreateContentType(array(
  176. 'type' => $content_type_machine,
  177. 'name' => $content_type_friendly,
  178. ));
  179. // Now add a singleton field.
  180. $single_field_name_friendly = $this->randomName(20);
  181. $single_field_name_machine = strtolower($this->randomName(10));
  182. //$single_field_name = 'field_'. $single_field_name_machine;
  183. $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
  184. // Okay, now we want to make sure this display is changed:
  185. $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
  186. $edit = array(
  187. 'fields[field_' . $single_field_name_machine . '][label]' => 'above',
  188. 'fields[field_' . $single_field_name_machine . '][type]' => 'link_plain',
  189. );
  190. $this->drupalPost(NULL, $edit, t('Save'));
  191. $this->createNodeTypeUser($content_type_machine);
  192. $link_tests = array(
  193. 'plain' => array(
  194. 'text' => 'Display',
  195. 'url' => 'http://www.example.com/',
  196. ),
  197. 'query' => array(
  198. 'text' => 'Display',
  199. 'url' => 'http://www.example.com/?q=test',
  200. ),
  201. 'fragment' => array(
  202. 'text' => 'Display',
  203. 'url' => 'http://www.example.com/#test',
  204. ),
  205. );
  206. foreach ($link_tests as $key => $link_test) {
  207. $link_text = $link_test['text'];
  208. $link_url = $link_test['url'];
  209. $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
  210. $this->assertText($link_url);
  211. $this->assertNoText($link_text);
  212. $this->assertNoLinkByHref($link_url);
  213. }
  214. }
  215. function testFormatterHost() {
  216. $content_type_friendly = $this->randomName(20);
  217. $content_type_machine = strtolower($this->randomName(10));
  218. $this->drupalCreateContentType(array(
  219. 'type' => $content_type_machine,
  220. 'name' => $content_type_friendly,
  221. ));
  222. // Now add a singleton field.
  223. $single_field_name_friendly = $this->randomName(20);
  224. $single_field_name_machine = strtolower($this->randomName(10));
  225. //$single_field_name = 'field_'. $single_field_name_machine;
  226. $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
  227. // Okay, now we want to make sure this display is changed:
  228. $this->drupalGet('admin/structure/types/manage/'. $content_type_machine .'/display');
  229. $edit = array(
  230. 'fields[field_'. $single_field_name_machine .'][label]' => 'above',
  231. 'fields[field_'. $single_field_name_machine .'][type]' => 'link_host',
  232. );
  233. $this->drupalPost(NULL, $edit, t('Save'));
  234. $this->createNodeTypeUser($content_type_machine);
  235. $link_text = 'Display';
  236. $link_url = 'http://www.example.com/';
  237. $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
  238. $this->assertText('www.example.com');
  239. $this->assertNoText($link_text);
  240. $this->assertNoLinkByHref($link_url);
  241. }
  242. function testFormatterURL() {
  243. $content_type_friendly = $this->randomName(20);
  244. $content_type_machine = strtolower($this->randomName(10));
  245. $this->drupalCreateContentType(array(
  246. 'type' => $content_type_machine,
  247. 'name' => $content_type_friendly,
  248. ));
  249. // Now add a singleton field.
  250. $single_field_name_friendly = $this->randomName(20);
  251. $single_field_name_machine = strtolower($this->randomName(10));
  252. //$single_field_name = 'field_'. $single_field_name_machine;
  253. $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
  254. // Okay, now we want to make sure this display is changed:
  255. $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
  256. $edit = array(
  257. 'fields[field_' . $single_field_name_machine . '][label]' => 'above',
  258. 'fields[field_' . $single_field_name_machine . '][type]' => 'link_url',
  259. );
  260. $this->drupalPost(NULL, $edit, t('Save'));
  261. $this->createNodeTypeUser($content_type_machine);
  262. $link_tests = array(
  263. 'plain' => array(
  264. 'text' => 'Display',
  265. 'url' => 'http://www.example.com/',
  266. ),
  267. 'query' => array(
  268. 'text' => 'Display',
  269. 'url' => 'http://www.example.com/?q=test',
  270. ),
  271. 'fragment' => array(
  272. 'text' => 'Display',
  273. 'url' => 'http://www.example.com/#test',
  274. ),
  275. );
  276. foreach ($link_tests as $key => $link_test) {
  277. $link_text = $link_test['text'];
  278. $link_url = $link_test['url'];
  279. $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
  280. $this->assertNoText($link_text);
  281. $this->assertLinkByHref($link_url);
  282. }
  283. }
  284. function testFormatterShort() {
  285. $content_type_friendly = $this->randomName(20);
  286. $content_type_machine = strtolower($this->randomName(10));
  287. $this->drupalCreateContentType(array(
  288. 'type' => $content_type_machine,
  289. 'name' => $content_type_friendly,
  290. ));
  291. // Now add a singleton field.
  292. $single_field_name_friendly = $this->randomName(20);
  293. $single_field_name_machine = strtolower($this->randomName(10));
  294. //$single_field_name = 'field_'. $single_field_name_machine;
  295. $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
  296. // Okay, now we want to make sure this display is changed:
  297. $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
  298. $edit = array(
  299. 'fields[field_' . $single_field_name_machine . '][label]' => 'above',
  300. 'fields[field_' . $single_field_name_machine . '][type]' => 'link_short',
  301. );
  302. $this->drupalPost(NULL, $edit, t('Save'));
  303. $this->createNodeTypeUser($content_type_machine);
  304. $link_tests = array(
  305. 'plain' => array(
  306. 'text' => 'Display',
  307. 'url' => 'http://www.example.com/',
  308. ),
  309. 'query' => array(
  310. 'text' => 'Display',
  311. 'url' => 'http://www.example.com/?q=test',
  312. ),
  313. 'fragment' => array(
  314. 'text' => 'Display',
  315. 'url' => 'http://www.example.com/#test',
  316. ),
  317. );
  318. foreach ($link_tests as $key => $link_test) {
  319. $link_text = $link_test['text'];
  320. $link_url = $link_test['url'];
  321. $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
  322. $this->assertText('Link');
  323. $this->assertNoText($link_text);
  324. $this->assertLinkByHref($link_url);
  325. }
  326. }
  327. function testFormatterLabel() {
  328. $content_type_friendly = $this->randomName(20);
  329. $content_type_machine = strtolower($this->randomName(10));
  330. $this->drupalCreateContentType(array(
  331. 'type' => $content_type_machine,
  332. 'name' => $content_type_friendly,
  333. ));
  334. // Now add a singleton field.
  335. $single_field_name_friendly = $this->randomName(20);
  336. $single_field_name_machine = strtolower($this->randomName(10));
  337. //$single_field_name = 'field_'. $single_field_name_machine;
  338. $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
  339. // Okay, now we want to make sure this display is changed:
  340. $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
  341. $edit = array(
  342. 'fields[field_' . $single_field_name_machine . '][label]' => 'above',
  343. 'fields[field_' . $single_field_name_machine . '][type]' => 'link_label',
  344. );
  345. $this->drupalPost(NULL, $edit, t('Save'));
  346. $this->createNodeTypeUser($content_type_machine);
  347. $link_tests = array(
  348. 'plain' => array(
  349. 'text' => 'Display',
  350. 'url' => 'http://www.example.com/',
  351. ),
  352. 'query' => array(
  353. 'text' => 'Display',
  354. 'url' => 'http://www.example.com/?q=test',
  355. ),
  356. 'fragment' => array(
  357. 'text' => 'Display',
  358. 'url' => 'http://www.example.com/#test',
  359. ),
  360. );
  361. foreach ($link_tests as $key => $link_test) {
  362. $link_text = $link_test['text'];
  363. $link_url = $link_test['url'];
  364. $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
  365. $this->assertNoText($link_text);
  366. $this->assertText($single_field_name_friendly);
  367. $this->assertLinkByHref($link_url);
  368. }
  369. }
  370. function testFormatterSeparate() {
  371. $content_type_friendly = $this->randomName(20);
  372. $content_type_machine = strtolower($this->randomName(10));
  373. $this->drupalCreateContentType(array(
  374. 'type' => $content_type_machine,
  375. 'name' => $content_type_friendly,
  376. ));
  377. // Now add a singleton field.
  378. $single_field_name_friendly = $this->randomName(20);
  379. $single_field_name_machine = strtolower($this->randomName(10));
  380. //$single_field_name = 'field_'. $single_field_name_machine;
  381. $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
  382. // Okay, now we want to make sure this display is changed:
  383. $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
  384. $edit = array(
  385. 'fields[field_' . $single_field_name_machine . '][label]' => 'above',
  386. 'fields[field_' . $single_field_name_machine . '][type]' => 'link_separate',
  387. );
  388. $this->drupalPost(NULL, $edit, t('Save'));
  389. $this->createNodeTypeUser($content_type_machine);
  390. $plain_url = 'http://www.example.com/';
  391. $link_tests = array(
  392. 'plain' => array(
  393. 'text' => $this->randomName(20),
  394. 'url' => $plain_url,
  395. ),
  396. 'query' => array(
  397. 'text' => $this->randomName(20),
  398. 'url' => $plain_url . '?q=test',
  399. ),
  400. 'fragment' => array(
  401. 'text' => $this->randomName(20),
  402. 'url' => $plain_url . '#test',
  403. ),
  404. );
  405. foreach ($link_tests as $key => $link_test) {
  406. $link_text = $link_test['text'];
  407. $link_url = $link_test['url'];
  408. $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
  409. $this->assertText($link_text);
  410. $this->assertLink($plain_url);
  411. $this->assertLinkByHref($link_url);
  412. }
  413. }
  414. function testFormatterPlainTitle() {
  415. $content_type_friendly = $this->randomName(20);
  416. $content_type_machine = strtolower($this->randomName(10));
  417. $this->drupalCreateContentType(array(
  418. 'type' => $content_type_machine,
  419. 'name' => $content_type_friendly,
  420. ));
  421. // Now add a singleton field.
  422. $single_field_name_friendly = $this->randomName(20);
  423. $single_field_name_machine = strtolower($this->randomName(10));
  424. //$single_field_name = 'field_'. $single_field_name_machine;
  425. $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
  426. // Okay, now we want to make sure this display is changed:
  427. $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
  428. $edit = array(
  429. 'fields[field_' . $single_field_name_machine . '][label]' => 'above',
  430. 'fields[field_' . $single_field_name_machine . '][type]' => 'link_title_plain',
  431. );
  432. $this->drupalPost(NULL, $edit, t('Save'));
  433. $this->createNodeTypeUser($content_type_machine);
  434. $link_text = 'Display';
  435. $link_url = 'http://www.example.com/';
  436. $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
  437. $this->assertText($link_text);
  438. $this->assertNoText($link_url);
  439. $this->assertNoLinkByHref($link_url);
  440. }
  441. }