link.attribute.test 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 testFormatterHost() {
  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_host',
  230. );
  231. $this->drupalPost(NULL, $edit, t('Save'));
  232. $this->createNodeTypeUser($content_type_machine);
  233. $link_text = 'Display';
  234. $link_url = 'http://www.example.com/';
  235. $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
  236. $this->assertText('www.example.com');
  237. $this->assertNoText($link_text);
  238. $this->assertNoLinkByHref($link_url);
  239. }
  240. function testFormatterURL() {
  241. $content_type_friendly = $this->randomName(20);
  242. $content_type_machine = strtolower($this->randomName(10));
  243. $this->drupalCreateContentType(array(
  244. 'type' => $content_type_machine,
  245. 'name' => $content_type_friendly,
  246. ));
  247. // Now add a singleton field.
  248. $single_field_name_friendly = $this->randomName(20);
  249. $single_field_name_machine = strtolower($this->randomName(10));
  250. //$single_field_name = 'field_'. $single_field_name_machine;
  251. $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
  252. // Okay, now we want to make sure this display is changed:
  253. $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
  254. $edit = array(
  255. 'fields[field_' . $single_field_name_machine . '][label]' => 'above',
  256. 'fields[field_' . $single_field_name_machine . '][type]' => 'link_url',
  257. );
  258. $this->drupalPost(NULL, $edit, t('Save'));
  259. $this->createNodeTypeUser($content_type_machine);
  260. $link_tests = array(
  261. 'plain' => array(
  262. 'text' => 'Display',
  263. 'url' => 'http://www.example.com/',
  264. ),
  265. 'query' => array(
  266. 'text' => 'Display',
  267. 'url' => 'http://www.example.com/?q=test',
  268. ),
  269. 'fragment' => array(
  270. 'text' => 'Display',
  271. 'url' => 'http://www.example.com/#test',
  272. ),
  273. );
  274. foreach ($link_tests as $key => $link_test) {
  275. $link_text = $link_test['text'];
  276. $link_url = $link_test['url'];
  277. $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
  278. $this->assertNoText($link_text);
  279. $this->assertLinkByHref($link_url);
  280. }
  281. }
  282. function testFormatterShort() {
  283. $content_type_friendly = $this->randomName(20);
  284. $content_type_machine = strtolower($this->randomName(10));
  285. $this->drupalCreateContentType(array(
  286. 'type' => $content_type_machine,
  287. 'name' => $content_type_friendly,
  288. ));
  289. // Now add a singleton field.
  290. $single_field_name_friendly = $this->randomName(20);
  291. $single_field_name_machine = strtolower($this->randomName(10));
  292. //$single_field_name = 'field_'. $single_field_name_machine;
  293. $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
  294. // Okay, now we want to make sure this display is changed:
  295. $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
  296. $edit = array(
  297. 'fields[field_' . $single_field_name_machine . '][label]' => 'above',
  298. 'fields[field_' . $single_field_name_machine . '][type]' => 'link_short',
  299. );
  300. $this->drupalPost(NULL, $edit, t('Save'));
  301. $this->createNodeTypeUser($content_type_machine);
  302. $link_tests = array(
  303. 'plain' => array(
  304. 'text' => 'Display',
  305. 'url' => 'http://www.example.com/',
  306. ),
  307. 'query' => array(
  308. 'text' => 'Display',
  309. 'url' => 'http://www.example.com/?q=test',
  310. ),
  311. 'fragment' => array(
  312. 'text' => 'Display',
  313. 'url' => 'http://www.example.com/#test',
  314. ),
  315. );
  316. foreach ($link_tests as $key => $link_test) {
  317. $link_text = $link_test['text'];
  318. $link_url = $link_test['url'];
  319. $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
  320. $this->assertText('Link');
  321. $this->assertNoText($link_text);
  322. $this->assertLinkByHref($link_url);
  323. }
  324. }
  325. function testFormatterLabel() {
  326. $content_type_friendly = $this->randomName(20);
  327. $content_type_machine = strtolower($this->randomName(10));
  328. $this->drupalCreateContentType(array(
  329. 'type' => $content_type_machine,
  330. 'name' => $content_type_friendly,
  331. ));
  332. // Now add a singleton field.
  333. $single_field_name_friendly = $this->randomName(20);
  334. $single_field_name_machine = strtolower($this->randomName(10));
  335. //$single_field_name = 'field_'. $single_field_name_machine;
  336. $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
  337. // Okay, now we want to make sure this display is changed:
  338. $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
  339. $edit = array(
  340. 'fields[field_' . $single_field_name_machine . '][label]' => 'above',
  341. 'fields[field_' . $single_field_name_machine . '][type]' => 'link_label',
  342. );
  343. $this->drupalPost(NULL, $edit, t('Save'));
  344. $this->createNodeTypeUser($content_type_machine);
  345. $link_tests = array(
  346. 'plain' => array(
  347. 'text' => 'Display',
  348. 'url' => 'http://www.example.com/',
  349. ),
  350. 'query' => array(
  351. 'text' => 'Display',
  352. 'url' => 'http://www.example.com/?q=test',
  353. ),
  354. 'fragment' => array(
  355. 'text' => 'Display',
  356. 'url' => 'http://www.example.com/#test',
  357. ),
  358. );
  359. foreach ($link_tests as $key => $link_test) {
  360. $link_text = $link_test['text'];
  361. $link_url = $link_test['url'];
  362. $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
  363. $this->assertNoText($link_text);
  364. $this->assertText($single_field_name_friendly);
  365. $this->assertLinkByHref($link_url);
  366. }
  367. }
  368. function testFormatterSeparate() {
  369. $content_type_friendly = $this->randomName(20);
  370. $content_type_machine = strtolower($this->randomName(10));
  371. $this->drupalCreateContentType(array(
  372. 'type' => $content_type_machine,
  373. 'name' => $content_type_friendly,
  374. ));
  375. // Now add a singleton field.
  376. $single_field_name_friendly = $this->randomName(20);
  377. $single_field_name_machine = strtolower($this->randomName(10));
  378. //$single_field_name = 'field_'. $single_field_name_machine;
  379. $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
  380. // Okay, now we want to make sure this display is changed:
  381. $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
  382. $edit = array(
  383. 'fields[field_' . $single_field_name_machine . '][label]' => 'above',
  384. 'fields[field_' . $single_field_name_machine . '][type]' => 'link_separate',
  385. );
  386. $this->drupalPost(NULL, $edit, t('Save'));
  387. $this->createNodeTypeUser($content_type_machine);
  388. $plain_url = 'http://www.example.com/';
  389. $link_tests = array(
  390. 'plain' => array(
  391. 'text' => $this->randomName(20),
  392. 'url' => $plain_url,
  393. ),
  394. 'query' => array(
  395. 'text' => $this->randomName(20),
  396. 'url' => $plain_url . '?q=test',
  397. ),
  398. 'fragment' => array(
  399. 'text' => $this->randomName(20),
  400. 'url' => $plain_url . '#test',
  401. ),
  402. );
  403. foreach ($link_tests as $key => $link_test) {
  404. $link_text = $link_test['text'];
  405. $link_url = $link_test['url'];
  406. $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
  407. $this->assertText($link_text);
  408. $this->assertLink($plain_url);
  409. $this->assertLinkByHref($link_url);
  410. }
  411. }
  412. function testFormatterPlainTitle() {
  413. $content_type_friendly = $this->randomName(20);
  414. $content_type_machine = strtolower($this->randomName(10));
  415. $this->drupalCreateContentType(array(
  416. 'type' => $content_type_machine,
  417. 'name' => $content_type_friendly,
  418. ));
  419. // Now add a singleton field.
  420. $single_field_name_friendly = $this->randomName(20);
  421. $single_field_name_machine = strtolower($this->randomName(10));
  422. //$single_field_name = 'field_'. $single_field_name_machine;
  423. $this->createSimpleLinkField($single_field_name_machine, $single_field_name_friendly, $content_type_machine);
  424. // Okay, now we want to make sure this display is changed:
  425. $this->drupalGet('admin/structure/types/manage/' . $content_type_machine . '/display');
  426. $edit = array(
  427. 'fields[field_' . $single_field_name_machine . '][label]' => 'above',
  428. 'fields[field_' . $single_field_name_machine . '][type]' => 'link_title_plain',
  429. );
  430. $this->drupalPost(NULL, $edit, t('Save'));
  431. $this->createNodeTypeUser($content_type_machine);
  432. $link_text = 'Display';
  433. $link_url = 'http://www.example.com/';
  434. $this->createNodeForTesting($content_type_machine, $content_type_friendly, $single_field_name_machine, $link_text, $link_url);
  435. $this->assertText($link_text);
  436. $this->assertNoText($link_url);
  437. $this->assertNoLinkByHref($link_url);
  438. }
  439. }