list.test 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. /**
  3. * @file
  4. * Tests for list.module.
  5. */
  6. /**
  7. * Tests for the 'List' field types.
  8. */
  9. class ListFieldTestCase extends FieldTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'List field',
  13. 'description' => 'Test the List field type.',
  14. 'group' => 'Field types',
  15. );
  16. }
  17. function setUp() {
  18. parent::setUp('field_test');
  19. $this->field_name = 'test_list';
  20. $this->field = array(
  21. 'field_name' => $this->field_name,
  22. 'type' => 'list_integer',
  23. 'cardinality' => 1,
  24. 'settings' => array(
  25. 'allowed_values' => array(1 => 'One', 2 => 'Two', 3 => 'Three'),
  26. ),
  27. );
  28. $this->field = field_create_field($this->field);
  29. $this->instance = array(
  30. 'field_name' => $this->field_name,
  31. 'entity_type' => 'test_entity',
  32. 'bundle' => 'test_bundle',
  33. 'widget' => array(
  34. 'type' => 'options_buttons',
  35. ),
  36. );
  37. $this->instance = field_create_instance($this->instance);
  38. }
  39. /**
  40. * Test that allowed values can be updated.
  41. */
  42. function testUpdateAllowedValues() {
  43. $langcode = LANGUAGE_NONE;
  44. // All three options appear.
  45. $entity = field_test_create_stub_entity();
  46. $form = drupal_get_form('field_test_entity_form', $entity);
  47. $this->assertTrue(!empty($form[$this->field_name][$langcode][1]), 'Option 1 exists');
  48. $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), 'Option 2 exists');
  49. $this->assertTrue(!empty($form[$this->field_name][$langcode][3]), 'Option 3 exists');
  50. // Use one of the values in an actual entity, and check that this value
  51. // cannot be removed from the list.
  52. $entity = field_test_create_stub_entity();
  53. $entity->{$this->field_name}[$langcode][0] = array('value' => 1);
  54. field_test_entity_save($entity);
  55. $this->field['settings']['allowed_values'] = array(2 => 'Two');
  56. try {
  57. field_update_field($this->field);
  58. $this->fail(t('Cannot update a list field to not include keys with existing data.'));
  59. }
  60. catch (FieldException $e) {
  61. $this->pass(t('Cannot update a list field to not include keys with existing data.'));
  62. }
  63. // Empty the value, so that we can actually remove the option.
  64. $entity->{$this->field_name}[$langcode] = array();
  65. field_test_entity_save($entity);
  66. // Removed options do not appear.
  67. $this->field['settings']['allowed_values'] = array(2 => 'Two');
  68. field_update_field($this->field);
  69. $entity = field_test_create_stub_entity();
  70. $form = drupal_get_form('field_test_entity_form', $entity);
  71. $this->assertTrue(empty($form[$this->field_name][$langcode][1]), 'Option 1 does not exist');
  72. $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), 'Option 2 exists');
  73. $this->assertTrue(empty($form[$this->field_name][$langcode][3]), 'Option 3 does not exist');
  74. // Completely new options appear.
  75. $this->field['settings']['allowed_values'] = array(10 => 'Update', 20 => 'Twenty');
  76. field_update_field($this->field);
  77. $form = drupal_get_form('field_test_entity_form', $entity);
  78. $this->assertTrue(empty($form[$this->field_name][$langcode][1]), 'Option 1 does not exist');
  79. $this->assertTrue(empty($form[$this->field_name][$langcode][2]), 'Option 2 does not exist');
  80. $this->assertTrue(empty($form[$this->field_name][$langcode][3]), 'Option 3 does not exist');
  81. $this->assertTrue(!empty($form[$this->field_name][$langcode][10]), 'Option 10 exists');
  82. $this->assertTrue(!empty($form[$this->field_name][$langcode][20]), 'Option 20 exists');
  83. // Options are reset when a new field with the same name is created.
  84. field_delete_field($this->field_name);
  85. unset($this->field['id']);
  86. $this->field['settings']['allowed_values'] = array(1 => 'One', 2 => 'Two', 3 => 'Three');
  87. $this->field = field_create_field($this->field);
  88. $this->instance = array(
  89. 'field_name' => $this->field_name,
  90. 'entity_type' => 'test_entity',
  91. 'bundle' => 'test_bundle',
  92. 'widget' => array(
  93. 'type' => 'options_buttons',
  94. ),
  95. );
  96. $this->instance = field_create_instance($this->instance);
  97. $entity = field_test_create_stub_entity();
  98. $form = drupal_get_form('field_test_entity_form', $entity);
  99. $this->assertTrue(!empty($form[$this->field_name][$langcode][1]), 'Option 1 exists');
  100. $this->assertTrue(!empty($form[$this->field_name][$langcode][2]), 'Option 2 exists');
  101. $this->assertTrue(!empty($form[$this->field_name][$langcode][3]), 'Option 3 exists');
  102. }
  103. }
  104. /**
  105. * Sets up a List field for testing allowed values functions.
  106. */
  107. class ListDynamicValuesTestCase extends FieldTestCase {
  108. function setUp() {
  109. parent::setUp(array('list', 'field_test', 'list_test'));
  110. $this->field_name = 'test_list';
  111. $this->field = array(
  112. 'field_name' => $this->field_name,
  113. 'type' => 'list_text',
  114. 'cardinality' => 1,
  115. 'settings' => array(
  116. 'allowed_values_function' => 'list_test_dynamic_values_callback',
  117. ),
  118. );
  119. $this->field = field_create_field($this->field);
  120. $this->instance = array(
  121. 'field_name' => $this->field_name,
  122. 'entity_type' => 'test_entity',
  123. 'bundle' => 'test_bundle',
  124. 'required' => TRUE,
  125. 'widget' => array(
  126. 'type' => 'options_select',
  127. ),
  128. );
  129. $this->instance = field_create_instance($this->instance);
  130. $this->test = array(
  131. 'id' => mt_rand(1, 10),
  132. // Make sure this does not equal the ID so that
  133. // list_test_dynamic_values_callback() always returns 4 values.
  134. 'vid' => mt_rand(20, 30),
  135. 'bundle' => 'test_bundle',
  136. 'label' => $this->randomName(),
  137. );
  138. $this->entity = call_user_func_array('field_test_create_stub_entity', $this->test);
  139. }
  140. }
  141. /**
  142. * Tests the List field allowed values function.
  143. */
  144. class ListDynamicValuesValidationTestCase extends ListDynamicValuesTestCase {
  145. public static function getInfo() {
  146. return array(
  147. 'name' => 'List field dynamic values',
  148. 'description' => 'Test the List field allowed values function.',
  149. 'group' => 'Field types',
  150. );
  151. }
  152. /**
  153. * Test that allowed values function gets the entity.
  154. */
  155. function testDynamicAllowedValues() {
  156. // Verify that the test passes against every value we had.
  157. foreach ($this->test as $key => $value) {
  158. $this->entity->test_list[LANGUAGE_NONE][0]['value'] = $value;
  159. try {
  160. field_attach_validate('test_entity', $this->entity);
  161. $this->pass("$key should pass");
  162. }
  163. catch (FieldValidationException $e) {
  164. // This will display as an exception, no need for a separate error.
  165. throw($e);
  166. }
  167. }
  168. // Now verify that the test does not pass against anything else.
  169. foreach ($this->test as $key => $value) {
  170. $this->entity->test_list[LANGUAGE_NONE][0]['value'] = is_numeric($value) ? (100 - $value) : ('X' . $value);
  171. $pass = FALSE;
  172. try {
  173. field_attach_validate('test_entity', $this->entity);
  174. }
  175. catch (FieldValidationException $e) {
  176. $pass = TRUE;
  177. }
  178. $this->assertTrue($pass, $key . ' should not pass');
  179. }
  180. }
  181. }
  182. /**
  183. * List module UI tests.
  184. */
  185. class ListFieldUITestCase extends FieldTestCase {
  186. public static function getInfo() {
  187. return array(
  188. 'name' => 'List field UI',
  189. 'description' => 'Test the List field UI functionality.',
  190. 'group' => 'Field types',
  191. );
  192. }
  193. function setUp() {
  194. parent::setUp('field_test', 'field_ui');
  195. // Create test user.
  196. $admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer taxonomy', 'administer fields'));
  197. $this->drupalLogin($admin_user);
  198. // Create content type, with underscores.
  199. $type_name = 'test_' . strtolower($this->randomName());
  200. $type = $this->drupalCreateContentType(array('name' => $type_name, 'type' => $type_name));
  201. $this->type = $type->type;
  202. // Store a valid URL name, with hyphens instead of underscores.
  203. $this->hyphen_type = str_replace('_', '-', $this->type);
  204. }
  205. /**
  206. * List (integer) : test 'allowed values' input.
  207. */
  208. function testListAllowedValuesInteger() {
  209. $this->field_name = 'field_list_integer';
  210. $this->createListField('list_integer');
  211. // Flat list of textual values.
  212. $string = "Zero\nOne";
  213. $array = array('0' => 'Zero', '1' => 'One');
  214. $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
  215. // Explicit integer keys.
  216. $string = "0|Zero\n2|Two";
  217. $array = array('0' => 'Zero', '2' => 'Two');
  218. $this->assertAllowedValuesInput($string, $array, 'Integer keys are accepted.');
  219. // Check that values can be added and removed.
  220. $string = "0|Zero\n1|One";
  221. $array = array('0' => 'Zero', '1' => 'One');
  222. $this->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');
  223. // Non-integer keys.
  224. $this->assertAllowedValuesInput("1.1|One", 'keys must be integers', 'Non integer keys are rejected.');
  225. $this->assertAllowedValuesInput("abc|abc", 'keys must be integers', 'Non integer keys are rejected.');
  226. // Mixed list of keyed and unkeyed values.
  227. $this->assertAllowedValuesInput("Zero\n1|One", 'invalid input', 'Mixed lists are rejected.');
  228. // Create a node with actual data for the field.
  229. $settings = array(
  230. 'type' => $this->type,
  231. $this->field_name => array(LANGUAGE_NONE => array(array('value' => 1))),
  232. );
  233. $node = $this->drupalCreateNode($settings);
  234. // Check that a flat list of values is rejected once the field has data.
  235. $this->assertAllowedValuesInput( "Zero\nOne", 'invalid input', 'Unkeyed lists are rejected once the field has data.');
  236. // Check that values can be added but values in use cannot be removed.
  237. $string = "0|Zero\n1|One\n2|Two";
  238. $array = array('0' => 'Zero', '1' => 'One', '2' => 'Two');
  239. $this->assertAllowedValuesInput($string, $array, 'Values can be added.');
  240. $string = "0|Zero\n1|One";
  241. $array = array('0' => 'Zero', '1' => 'One');
  242. $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
  243. $this->assertAllowedValuesInput("0|Zero", 'some values are being removed while currently in use', 'Values in use cannot be removed.');
  244. // Delete the node, remove the value.
  245. node_delete($node->nid);
  246. $string = "0|Zero";
  247. $array = array('0' => 'Zero');
  248. $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
  249. }
  250. /**
  251. * List (float) : test 'allowed values' input.
  252. */
  253. function testListAllowedValuesFloat() {
  254. $this->field_name = 'field_list_float';
  255. $this->createListField('list_float');
  256. // Flat list of textual values.
  257. $string = "Zero\nOne";
  258. $array = array('0' => 'Zero', '1' => 'One');
  259. $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
  260. // Explicit numeric keys.
  261. $string = "0|Zero\n.5|Point five";
  262. $array = array('0' => 'Zero', '0.5' => 'Point five');
  263. $this->assertAllowedValuesInput($string, $array, 'Integer keys are accepted.');
  264. // Check that values can be added and removed.
  265. $string = "0|Zero\n.5|Point five\n1.0|One";
  266. $array = array('0' => 'Zero', '0.5' => 'Point five', '1' => 'One');
  267. $this->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');
  268. // Non-numeric keys.
  269. $this->assertAllowedValuesInput("abc|abc\n", 'each key must be a valid integer or decimal', 'Non numeric keys are rejected.');
  270. // Mixed list of keyed and unkeyed values.
  271. $this->assertAllowedValuesInput("Zero\n1|One\n", 'invalid input', 'Mixed lists are rejected.');
  272. // Create a node with actual data for the field.
  273. $settings = array(
  274. 'type' => $this->type,
  275. $this->field_name => array(LANGUAGE_NONE => array(array('value' => .5))),
  276. );
  277. $node = $this->drupalCreateNode($settings);
  278. // Check that a flat list of values is rejected once the field has data.
  279. $this->assertAllowedValuesInput("Zero\nOne", 'invalid input', 'Unkeyed lists are rejected once the field has data.');
  280. // Check that values can be added but values in use cannot be removed.
  281. $string = "0|Zero\n.5|Point five\n2|Two";
  282. $array = array('0' => 'Zero', '0.5' => 'Point five', '2' => 'Two');
  283. $this->assertAllowedValuesInput($string, $array, 'Values can be added.');
  284. $string = "0|Zero\n.5|Point five";
  285. $array = array('0' => 'Zero', '0.5' => 'Point five');
  286. $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
  287. $this->assertAllowedValuesInput("0|Zero", 'some values are being removed while currently in use', 'Values in use cannot be removed.');
  288. // Delete the node, remove the value.
  289. node_delete($node->nid);
  290. $string = "0|Zero";
  291. $array = array('0' => 'Zero');
  292. $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
  293. }
  294. /**
  295. * List (text) : test 'allowed values' input.
  296. */
  297. function testListAllowedValuesText() {
  298. $this->field_name = 'field_list_text';
  299. $this->createListField('list_text');
  300. // Flat list of textual values.
  301. $string = "Zero\nOne";
  302. $array = array('Zero' => 'Zero', 'One' => 'One');
  303. $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are accepted.');
  304. // Explicit keys.
  305. $string = "zero|Zero\none|One";
  306. $array = array('zero' => 'Zero', 'one' => 'One');
  307. $this->assertAllowedValuesInput($string, $array, 'Explicit keys are accepted.');
  308. // Check that values can be added and removed.
  309. $string = "zero|Zero\ntwo|Two";
  310. $array = array('zero' => 'Zero', 'two' => 'Two');
  311. $this->assertAllowedValuesInput($string, $array, 'Values can be added and removed.');
  312. // Mixed list of keyed and unkeyed values.
  313. $string = "zero|Zero\nOne\n";
  314. $array = array('zero' => 'Zero', 'One' => 'One');
  315. $this->assertAllowedValuesInput($string, $array, 'Mixed lists are accepted.');
  316. // Overly long keys.
  317. $this->assertAllowedValuesInput("zero|Zero\n" . $this->randomName(256) . "|One", 'each key must be a string at most 255 characters long', 'Overly long keys are rejected.');
  318. // Create a node with actual data for the field.
  319. $settings = array(
  320. 'type' => $this->type,
  321. $this->field_name => array(LANGUAGE_NONE => array(array('value' => 'One'))),
  322. );
  323. $node = $this->drupalCreateNode($settings);
  324. // Check that flat lists of values are still accepted once the field has
  325. // data.
  326. $string = "Zero\nOne";
  327. $array = array('Zero' => 'Zero', 'One' => 'One');
  328. $this->assertAllowedValuesInput($string, $array, 'Unkeyed lists are still accepted once the field has data.');
  329. // Check that values can be added but values in use cannot be removed.
  330. $string = "Zero\nOne\nTwo";
  331. $array = array('Zero' => 'Zero', 'One' => 'One', 'Two' => 'Two');
  332. $this->assertAllowedValuesInput($string, $array, 'Values can be added.');
  333. $string = "Zero\nOne";
  334. $array = array('Zero' => 'Zero', 'One' => 'One');
  335. $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
  336. $this->assertAllowedValuesInput("Zero", 'some values are being removed while currently in use', 'Values in use cannot be removed.');
  337. // Delete the node, remove the value.
  338. node_delete($node->nid);
  339. $string = "Zero";
  340. $array = array('Zero' => 'Zero');
  341. $this->assertAllowedValuesInput($string, $array, 'Values not in use can be removed.');
  342. }
  343. /**
  344. * List (boolen) : test 'On/Off' values input.
  345. */
  346. function testListAllowedValuesBoolean() {
  347. $this->field_name = 'field_list_boolean';
  348. $this->createListField('list_boolean');
  349. // Check that the separate 'On' and 'Off' form fields work.
  350. $on = $this->randomName();
  351. $off = $this->randomName();
  352. $allowed_values = array(1 => $on, 0 => $off);
  353. $edit = array(
  354. 'on' => $on,
  355. 'off' => $off,
  356. );
  357. $this->drupalPost($this->admin_path, $edit, t('Save settings'));
  358. $this->assertText("Saved field_list_boolean configuration.", "The 'On' and 'Off' form fields work for boolean fields.");
  359. // Test the allowed_values on the field settings form.
  360. $this->drupalGet($this->admin_path);
  361. $this->assertFieldByName('on', $on, "The 'On' value is stored correctly.");
  362. $this->assertFieldByName('off', $off, "The 'Off' value is stored correctly.");
  363. $field = field_info_field($this->field_name);
  364. $this->assertEqual($field['settings']['allowed_values'], $allowed_values, 'The allowed value is correct');
  365. $this->assertFalse(isset($field['settings']['on']), 'The on value is not saved into settings');
  366. $this->assertFalse(isset($field['settings']['off']), 'The off value is not saved into settings');
  367. }
  368. /**
  369. * Helper function to create list field of a given type.
  370. *
  371. * @param string $type
  372. * 'list_integer', 'list_float', 'list_text' or 'list_boolean'
  373. */
  374. protected function createListField($type) {
  375. // Create a test field and instance.
  376. $field = array(
  377. 'field_name' => $this->field_name,
  378. 'type' => $type,
  379. );
  380. field_create_field($field);
  381. $instance = array(
  382. 'field_name' => $this->field_name,
  383. 'entity_type' => 'node',
  384. 'bundle' => $this->type,
  385. );
  386. field_create_instance($instance);
  387. $this->admin_path = 'admin/structure/types/manage/' . $this->hyphen_type . '/fields/' . $this->field_name;
  388. }
  389. /**
  390. * Tests a string input for the 'allowed values' form element.
  391. *
  392. * @param $input_string
  393. * The input string, in the pipe-linefeed format expected by the form
  394. * element.
  395. * @param $result
  396. * Either an expected resulting array in
  397. * $field['settings']['allowed_values'], or an expected error message.
  398. * @param $message
  399. * Message to display.
  400. */
  401. function assertAllowedValuesInput($input_string, $result, $message) {
  402. $edit = array('field[settings][allowed_values]' => $input_string);
  403. $this->drupalPost($this->admin_path, $edit, t('Save settings'));
  404. if (is_string($result)) {
  405. $this->assertText($result, $message);
  406. }
  407. else {
  408. field_info_cache_clear();
  409. $field = field_info_field($this->field_name);
  410. $this->assertIdentical($field['settings']['allowed_values'], $result, $message);
  411. }
  412. }
  413. }