link.attribute.test 20 KB

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