link.attribute.test 18 KB

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