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