options.test 23 KB

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