options.test 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <?php
  2. /**
  3. * @file
  4. * Tests for options.module.
  5. */
  6. class OptionsWidgetsTestCase extends FieldTestCase {
  7. public static function getInfo() {
  8. return array(
  9. 'name' => 'Options widgets',
  10. 'description' => "Test the Options widgets.",
  11. 'group' => 'Field types'
  12. );
  13. }
  14. function setUp() {
  15. parent::setUp('field_test', 'list_test');
  16. // Field with cardinality 1.
  17. $this->card_1 = array(
  18. 'field_name' => 'card_1',
  19. 'type' => 'list_integer',
  20. 'cardinality' => 1,
  21. 'settings' => array(
  22. // Make sure that 0 works as an option.
  23. 'allowed_values' => array(0 => 'Zero', 1 => 'One', 2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>', 3 => 'Some HTML encoded markup with &lt; &amp; &gt;'),
  24. ),
  25. );
  26. $this->card_1 = field_create_field($this->card_1);
  27. // Field with cardinality 2.
  28. $this->card_2 = array(
  29. 'field_name' => 'card_2',
  30. 'type' => 'list_integer',
  31. 'cardinality' => 2,
  32. 'settings' => array(
  33. // Make sure that 0 works as an option.
  34. 'allowed_values' => array(0 => 'Zero', 1 => 'One', 2 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>'),
  35. ),
  36. );
  37. $this->card_2 = field_create_field($this->card_2);
  38. // Boolean field.
  39. $this->bool = array(
  40. 'field_name' => 'bool',
  41. 'type' => 'list_boolean',
  42. 'cardinality' => 1,
  43. 'settings' => array(
  44. // Make sure that 0 works as a 'on' value'.
  45. 'allowed_values' => array(1 => 'Zero', 0 => 'Some <script>dangerous</script> & unescaped <strong>markup</strong>'),
  46. ),
  47. );
  48. $this->bool = field_create_field($this->bool);
  49. // Create a web user.
  50. $this->web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer fields'));
  51. $this->drupalLogin($this->web_user);
  52. }
  53. /**
  54. * Tests the 'options_buttons' widget (single select).
  55. */
  56. function testRadioButtons() {
  57. // Create an instance of the 'single value' field.
  58. $instance = array(
  59. 'field_name' => $this->card_1['field_name'],
  60. 'entity_type' => 'test_entity',
  61. 'bundle' => 'test_bundle',
  62. 'widget' => array(
  63. 'type' => 'options_buttons',
  64. ),
  65. );
  66. $instance = field_create_instance($instance);
  67. $langcode = LANGUAGE_NONE;
  68. // Create an entity.
  69. $entity_init = field_test_create_stub_entity();
  70. $entity = clone $entity_init;
  71. $entity->is_new = TRUE;
  72. field_test_entity_save($entity);
  73. // With no field data, no buttons are checked.
  74. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  75. $this->assertNoFieldChecked("edit-card-1-$langcode-0");
  76. $this->assertNoFieldChecked("edit-card-1-$langcode-1");
  77. $this->assertNoFieldChecked("edit-card-1-$langcode-2");
  78. $this->assertRaw('Some dangerous &amp; unescaped <strong>markup</strong>', 'Option text was properly filtered.');
  79. // Select first option.
  80. $edit = array("card_1[$langcode]" => 0);
  81. $this->drupalPost(NULL, $edit, t('Save'));
  82. $this->assertFieldValues($entity_init, 'card_1', $langcode, array(0));
  83. // Check that the selected button is checked.
  84. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  85. $this->assertFieldChecked("edit-card-1-$langcode-0");
  86. $this->assertNoFieldChecked("edit-card-1-$langcode-1");
  87. $this->assertNoFieldChecked("edit-card-1-$langcode-2");
  88. // Unselect option.
  89. $edit = array("card_1[$langcode]" => '_none');
  90. $this->drupalPost(NULL, $edit, t('Save'));
  91. $this->assertFieldValues($entity_init, 'card_1', $langcode, array());
  92. // Check that required radios with one option is auto-selected.
  93. $this->card_1['settings']['allowed_values'] = array(99 => 'Only allowed value');
  94. field_update_field($this->card_1);
  95. $instance['required'] = TRUE;
  96. field_update_instance($instance);
  97. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  98. $this->assertFieldChecked("edit-card-1-$langcode-99");
  99. }
  100. /**
  101. * Tests the 'options_buttons' widget (multiple select).
  102. */
  103. function testCheckBoxes() {
  104. // Create an instance of the 'multiple values' field.
  105. $instance = array(
  106. 'field_name' => $this->card_2['field_name'],
  107. 'entity_type' => 'test_entity',
  108. 'bundle' => 'test_bundle',
  109. 'widget' => array(
  110. 'type' => 'options_buttons',
  111. ),
  112. );
  113. $instance = field_create_instance($instance);
  114. $langcode = LANGUAGE_NONE;
  115. // Create an entity.
  116. $entity_init = field_test_create_stub_entity();
  117. $entity = clone $entity_init;
  118. $entity->is_new = TRUE;
  119. field_test_entity_save($entity);
  120. // Display form: with no field data, nothing is checked.
  121. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  122. $this->assertNoFieldChecked("edit-card-2-$langcode-0");
  123. $this->assertNoFieldChecked("edit-card-2-$langcode-1");
  124. $this->assertNoFieldChecked("edit-card-2-$langcode-2");
  125. $this->assertRaw('Some dangerous &amp; unescaped <strong>markup</strong>', 'Option text was properly filtered.');
  126. // Submit form: select first and third options.
  127. $edit = array(
  128. "card_2[$langcode][0]" => TRUE,
  129. "card_2[$langcode][1]" => FALSE,
  130. "card_2[$langcode][2]" => TRUE,
  131. );
  132. $this->drupalPost(NULL, $edit, t('Save'));
  133. $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0, 2));
  134. // Display form: check that the right options are selected.
  135. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  136. $this->assertFieldChecked("edit-card-2-$langcode-0");
  137. $this->assertNoFieldChecked("edit-card-2-$langcode-1");
  138. $this->assertFieldChecked("edit-card-2-$langcode-2");
  139. // Submit form: select only first option.
  140. $edit = array(
  141. "card_2[$langcode][0]" => TRUE,
  142. "card_2[$langcode][1]" => FALSE,
  143. "card_2[$langcode][2]" => FALSE,
  144. );
  145. $this->drupalPost(NULL, $edit, t('Save'));
  146. $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
  147. // Display form: check that the right options are selected.
  148. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  149. $this->assertFieldChecked("edit-card-2-$langcode-0");
  150. $this->assertNoFieldChecked("edit-card-2-$langcode-1");
  151. $this->assertNoFieldChecked("edit-card-2-$langcode-2");
  152. // Submit form: select the three options while the field accepts only 2.
  153. $edit = array(
  154. "card_2[$langcode][0]" => TRUE,
  155. "card_2[$langcode][1]" => TRUE,
  156. "card_2[$langcode][2]" => TRUE,
  157. );
  158. $this->drupalPost(NULL, $edit, t('Save'));
  159. $this->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.');
  160. // Submit form: uncheck all options.
  161. $edit = array(
  162. "card_2[$langcode][0]" => FALSE,
  163. "card_2[$langcode][1]" => FALSE,
  164. "card_2[$langcode][2]" => FALSE,
  165. );
  166. $this->drupalPost(NULL, $edit, t('Save'));
  167. // Check that the value was saved.
  168. $this->assertFieldValues($entity_init, 'card_2', $langcode, array());
  169. // Required checkbox with one option is auto-selected.
  170. $this->card_2['settings']['allowed_values'] = array(99 => 'Only allowed value');
  171. field_update_field($this->card_2);
  172. $instance['required'] = TRUE;
  173. field_update_instance($instance);
  174. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  175. $this->assertFieldChecked("edit-card-2-$langcode-99");
  176. }
  177. /**
  178. * Tests the 'options_select' widget (single select).
  179. */
  180. function testSelectListSingle() {
  181. // Create an instance of the 'single value' field.
  182. $instance = array(
  183. 'field_name' => $this->card_1['field_name'],
  184. 'entity_type' => 'test_entity',
  185. 'bundle' => 'test_bundle',
  186. 'required' => TRUE,
  187. 'widget' => array(
  188. 'type' => 'options_select',
  189. ),
  190. );
  191. $instance = field_create_instance($instance);
  192. $langcode = LANGUAGE_NONE;
  193. // Create an entity.
  194. $entity_init = field_test_create_stub_entity();
  195. $entity = clone $entity_init;
  196. $entity->is_new = TRUE;
  197. field_test_entity_save($entity);
  198. // Display form.
  199. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  200. // A required field without any value has a "none" option.
  201. $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(':id' => 'edit-card-1-' . $langcode, ':label' => t('- Select a value -'))), 'A required select list has a "Select a value" choice.');
  202. // With no field data, nothing is selected.
  203. $this->assertNoOptionSelected("edit-card-1-$langcode", '_none');
  204. $this->assertNoOptionSelected("edit-card-1-$langcode", 0);
  205. $this->assertNoOptionSelected("edit-card-1-$langcode", 1);
  206. $this->assertNoOptionSelected("edit-card-1-$langcode", 2);
  207. $this->assertRaw('Some dangerous &amp; unescaped markup', 'Option text was properly filtered.');
  208. $this->assertRaw('Some HTML encoded markup with &lt; &amp; &gt;', 'HTML entities in option text were properly handled and not double-encoded');
  209. // Submit form: select invalid 'none' option.
  210. $edit = array("card_1[$langcode]" => '_none');
  211. $this->drupalPost(NULL, $edit, t('Save'));
  212. $this->assertRaw(t('!title field is required.', array('!title' => $instance['field_name'])), 'Cannot save a required field when selecting "none" from the select list.');
  213. // Submit form: select first option.
  214. $edit = array("card_1[$langcode]" => 0);
  215. $this->drupalPost(NULL, $edit, t('Save'));
  216. $this->assertFieldValues($entity_init, 'card_1', $langcode, array(0));
  217. // Display form: check that the right options are selected.
  218. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  219. // A required field with a value has no 'none' option.
  220. $this->assertFalse($this->xpath('//select[@id=:id]//option[@value="_none"]', array(':id' => 'edit-card-1-' . $langcode)), 'A required select list with an actual value has no "none" choice.');
  221. $this->assertOptionSelected("edit-card-1-$langcode", 0);
  222. $this->assertNoOptionSelected("edit-card-1-$langcode", 1);
  223. $this->assertNoOptionSelected("edit-card-1-$langcode", 2);
  224. // Make the field non required.
  225. $instance['required'] = FALSE;
  226. field_update_instance($instance);
  227. // Display form.
  228. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  229. // A non-required field has a 'none' option.
  230. $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none" and text()=:label]', array(':id' => 'edit-card-1-' . $langcode, ':label' => t('- None -'))), 'A non-required select list has a "None" choice.');
  231. // Submit form: Unselect the option.
  232. $edit = array("card_1[$langcode]" => '_none');
  233. $this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
  234. $this->assertFieldValues($entity_init, 'card_1', $langcode, array());
  235. // Test optgroups.
  236. $this->card_1['settings']['allowed_values'] = array();
  237. $this->card_1['settings']['allowed_values_function'] = 'list_test_allowed_values_callback';
  238. field_update_field($this->card_1);
  239. // Display form: with no field data, nothing is selected
  240. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  241. $this->assertNoOptionSelected("edit-card-1-$langcode", 0);
  242. $this->assertNoOptionSelected("edit-card-1-$langcode", 1);
  243. $this->assertNoOptionSelected("edit-card-1-$langcode", 2);
  244. $this->assertRaw('Some dangerous &amp; unescaped markup', 'Option text was properly filtered.');
  245. $this->assertRaw('Group 1', 'Option groups are displayed.');
  246. // Submit form: select first option.
  247. $edit = array("card_1[$langcode]" => 0);
  248. $this->drupalPost(NULL, $edit, t('Save'));
  249. $this->assertFieldValues($entity_init, 'card_1', $langcode, array(0));
  250. // Display form: check that the right options are selected.
  251. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  252. $this->assertOptionSelected("edit-card-1-$langcode", 0);
  253. $this->assertNoOptionSelected("edit-card-1-$langcode", 1);
  254. $this->assertNoOptionSelected("edit-card-1-$langcode", 2);
  255. // Submit form: Unselect the option.
  256. $edit = array("card_1[$langcode]" => '_none');
  257. $this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
  258. $this->assertFieldValues($entity_init, 'card_1', $langcode, array());
  259. }
  260. /**
  261. * Tests the 'options_select' widget (multiple select).
  262. */
  263. function testSelectListMultiple() {
  264. // Create an instance of the 'multiple values' field.
  265. $instance = array(
  266. 'field_name' => $this->card_2['field_name'],
  267. 'entity_type' => 'test_entity',
  268. 'bundle' => 'test_bundle',
  269. 'widget' => array(
  270. 'type' => 'options_select',
  271. ),
  272. );
  273. $instance = field_create_instance($instance);
  274. $langcode = LANGUAGE_NONE;
  275. // Create an entity.
  276. $entity_init = field_test_create_stub_entity();
  277. $entity = clone $entity_init;
  278. $entity->is_new = TRUE;
  279. field_test_entity_save($entity);
  280. // Display form: with no field data, nothing is selected.
  281. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  282. $this->assertNoOptionSelected("edit-card-2-$langcode", 0);
  283. $this->assertNoOptionSelected("edit-card-2-$langcode", 1);
  284. $this->assertNoOptionSelected("edit-card-2-$langcode", 2);
  285. $this->assertRaw('Some dangerous &amp; unescaped markup', 'Option text was properly filtered.');
  286. // Submit form: select first and third options.
  287. $edit = array("card_2[$langcode][]" => array(0 => 0, 2 => 2));
  288. $this->drupalPost(NULL, $edit, t('Save'));
  289. $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0, 2));
  290. // Display form: check that the right options are selected.
  291. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  292. $this->assertOptionSelected("edit-card-2-$langcode", 0);
  293. $this->assertNoOptionSelected("edit-card-2-$langcode", 1);
  294. $this->assertOptionSelected("edit-card-2-$langcode", 2);
  295. // Submit form: select only first option.
  296. $edit = array("card_2[$langcode][]" => array(0 => 0));
  297. $this->drupalPost(NULL, $edit, t('Save'));
  298. $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
  299. // Display form: check that the right options are selected.
  300. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  301. $this->assertOptionSelected("edit-card-2-$langcode", 0);
  302. $this->assertNoOptionSelected("edit-card-2-$langcode", 1);
  303. $this->assertNoOptionSelected("edit-card-2-$langcode", 2);
  304. // Submit form: select the three options while the field accepts only 2.
  305. $edit = array("card_2[$langcode][]" => array(0 => 0, 1 => 1, 2 => 2));
  306. $this->drupalPost(NULL, $edit, t('Save'));
  307. $this->assertText('this field cannot hold more than 2 values', 'Validation error was displayed.');
  308. // Submit form: uncheck all options.
  309. $edit = array("card_2[$langcode][]" => array());
  310. $this->drupalPost(NULL, $edit, t('Save'));
  311. $this->assertFieldValues($entity_init, 'card_2', $langcode, array());
  312. // Test the 'None' option.
  313. // Check that the 'none' option has no effect if actual options are selected
  314. // as well.
  315. $edit = array("card_2[$langcode][]" => array('_none' => '_none', 0 => 0));
  316. $this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
  317. $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
  318. // Check that selecting the 'none' option empties the field.
  319. $edit = array("card_2[$langcode][]" => array('_none' => '_none'));
  320. $this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
  321. $this->assertFieldValues($entity_init, 'card_2', $langcode, array());
  322. // A required select list does not have an empty key.
  323. $instance['required'] = TRUE;
  324. field_update_instance($instance);
  325. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  326. $this->assertFalse($this->xpath('//select[@id=:id]//option[@value=""]', array(':id' => 'edit-card-2-' . $langcode)), 'A required select list does not have an empty key.');
  327. // We do not have to test that a required select list with one option is
  328. // auto-selected because the browser does it for us.
  329. // Test optgroups.
  330. // Use a callback function defining optgroups.
  331. $this->card_2['settings']['allowed_values'] = array();
  332. $this->card_2['settings']['allowed_values_function'] = 'list_test_allowed_values_callback';
  333. field_update_field($this->card_2);
  334. $instance['required'] = FALSE;
  335. field_update_instance($instance);
  336. // Display form: with no field data, nothing is selected.
  337. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  338. $this->assertNoOptionSelected("edit-card-2-$langcode", 0);
  339. $this->assertNoOptionSelected("edit-card-2-$langcode", 1);
  340. $this->assertNoOptionSelected("edit-card-2-$langcode", 2);
  341. $this->assertRaw('Some dangerous &amp; unescaped markup', 'Option text was properly filtered.');
  342. $this->assertRaw('Group 1', 'Option groups are displayed.');
  343. // Submit form: select first option.
  344. $edit = array("card_2[$langcode][]" => array(0 => 0));
  345. $this->drupalPost(NULL, $edit, t('Save'));
  346. $this->assertFieldValues($entity_init, 'card_2', $langcode, array(0));
  347. // Display form: check that the right options are selected.
  348. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  349. $this->assertOptionSelected("edit-card-2-$langcode", 0);
  350. $this->assertNoOptionSelected("edit-card-2-$langcode", 1);
  351. $this->assertNoOptionSelected("edit-card-2-$langcode", 2);
  352. // Submit form: Unselect the option.
  353. $edit = array("card_2[$langcode][]" => array('_none' => '_none'));
  354. $this->drupalPost('test-entity/manage/' . $entity->ftid . '/edit', $edit, t('Save'));
  355. $this->assertFieldValues($entity_init, 'card_2', $langcode, array());
  356. }
  357. /**
  358. * Tests the 'options_onoff' widget.
  359. */
  360. function testOnOffCheckbox() {
  361. // Create an instance of the 'boolean' field.
  362. $instance = array(
  363. 'field_name' => $this->bool['field_name'],
  364. 'entity_type' => 'test_entity',
  365. 'bundle' => 'test_bundle',
  366. 'widget' => array(
  367. 'type' => 'options_onoff',
  368. ),
  369. );
  370. $instance = field_create_instance($instance);
  371. $langcode = LANGUAGE_NONE;
  372. // Create an entity.
  373. $entity_init = field_test_create_stub_entity();
  374. $entity = clone $entity_init;
  375. $entity->is_new = TRUE;
  376. field_test_entity_save($entity);
  377. // Display form: with no field data, option is unchecked.
  378. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  379. $this->assertNoFieldChecked("edit-bool-$langcode");
  380. $this->assertRaw('Some dangerous &amp; unescaped <strong>markup</strong>', 'Option text was properly filtered.');
  381. // Submit form: check the option.
  382. $edit = array("bool[$langcode]" => TRUE);
  383. $this->drupalPost(NULL, $edit, t('Save'));
  384. $this->assertFieldValues($entity_init, 'bool', $langcode, array(0));
  385. // Display form: check that the right options are selected.
  386. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  387. $this->assertFieldChecked("edit-bool-$langcode");
  388. // Submit form: uncheck the option.
  389. $edit = array("bool[$langcode]" => FALSE);
  390. $this->drupalPost(NULL, $edit, t('Save'));
  391. $this->assertFieldValues($entity_init, 'bool', $langcode, array(1));
  392. // Display form: with 'off' value, option is unchecked.
  393. $this->drupalGet('test-entity/manage/' . $entity->ftid . '/edit');
  394. $this->assertNoFieldChecked("edit-bool-$langcode");
  395. // Create admin user.
  396. $admin_user = $this->drupalCreateUser(array('access content', 'administer content types', 'administer taxonomy', 'administer fields'));
  397. $this->drupalLogin($admin_user);
  398. // Create a test field instance.
  399. $fieldUpdate = $this->bool;
  400. $fieldUpdate['settings']['allowed_values'] = array(0 => 0, 1 => 'MyOnValue');
  401. field_update_field($fieldUpdate);
  402. $instance = array(
  403. 'field_name' => $this->bool['field_name'],
  404. 'entity_type' => 'node',
  405. 'bundle' => 'page',
  406. 'widget' => array(
  407. 'type' => 'options_onoff',
  408. 'module' => 'options',
  409. ),
  410. );
  411. field_create_instance($instance);
  412. // Go to the edit page and check if the default settings works as expected
  413. $fieldEditUrl = 'admin/structure/types/manage/page/fields/bool';
  414. $this->drupalGet($fieldEditUrl);
  415. $this->assertText(
  416. 'Use field label instead of the "On value" as label ',
  417. 'Display setting checkbox available.'
  418. );
  419. $this->assertFieldByXPath(
  420. '*//label[@for="edit-' . $this->bool['field_name'] . '-und" and text()="MyOnValue "]',
  421. TRUE,
  422. 'Default case shows "On value"'
  423. );
  424. // Enable setting
  425. $edit = array('instance[widget][settings][display_label]' => 1);
  426. // Save the new Settings
  427. $this->drupalPost($fieldEditUrl, $edit, t('Save settings'));
  428. // Go again to the edit page and check if the setting
  429. // is stored and has the expected effect
  430. $this->drupalGet($fieldEditUrl);
  431. $this->assertText(
  432. 'Use field label instead of the "On value" as label ',
  433. 'Display setting checkbox is available'
  434. );
  435. $this->assertFieldChecked(
  436. 'edit-instance-widget-settings-display-label',
  437. 'Display settings checkbox checked'
  438. );
  439. $this->assertFieldByXPath(
  440. '*//label[@for="edit-' . $this->bool['field_name'] . '-und" and text()="' . $this->bool['field_name'] . ' "]',
  441. TRUE,
  442. 'Display label changes label of the checkbox'
  443. );
  444. }
  445. }
  446. /**
  447. * Test an options select on a list field with a dynamic allowed values function.
  448. */
  449. class OptionsSelectDynamicValuesTestCase extends ListDynamicValuesTestCase {
  450. public static function getInfo() {
  451. return array(
  452. 'name' => 'Options select dynamic values',
  453. 'description' => 'Test an options select on a list field with a dynamic allowed values function.',
  454. 'group' => 'Field types',
  455. );
  456. }
  457. /**
  458. * Tests the 'options_select' widget (single select).
  459. */
  460. function testSelectListDynamic() {
  461. // Create an entity.
  462. $this->entity->is_new = TRUE;
  463. field_test_entity_save($this->entity);
  464. // Create a web user.
  465. $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
  466. $this->drupalLogin($web_user);
  467. // Display form.
  468. $this->drupalGet('test-entity/manage/' . $this->entity->ftid . '/edit');
  469. $options = $this->xpath('//select[@id="edit-test-list-und"]/option');
  470. $this->assertEqual(count($options), count($this->test) + 1);
  471. foreach ($options as $option) {
  472. $value = (string) $option['value'];
  473. if ($value != '_none') {
  474. $this->assertTrue(array_search($value, $this->test));
  475. }
  476. }
  477. }
  478. }