title.test 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the Title module.
  5. */
  6. /**
  7. * Tests for legacy field replacement.
  8. */
  9. class TitleFieldReplacementTestCase extends DrupalWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Field replacement',
  13. 'description' => 'Test field replacement.',
  14. 'group' => 'Title',
  15. );
  16. }
  17. function setUp() {
  18. parent::setUp('entity', 'field_test', 'title', 'title_test');
  19. }
  20. /**
  21. * Test field replacement API and workflow.
  22. */
  23. function testFieldReplacementWorkflow() {
  24. $info = entity_get_info('test_entity');
  25. $label_key = $info['entity keys']['label'];
  26. $field_name = $label_key . '_field';
  27. // Enable field replacement for the test entity.
  28. title_field_replacement_toggle('test_entity', 'test_bundle', $label_key);
  29. $i = 0;
  30. $entity = field_test_create_stub_entity(FALSE, FALSE);
  31. while ($i++ <= 1) {
  32. // The first time the entity gets created the second time gets updated.
  33. title_test_entity_save($entity);
  34. // Check that the replacing field value has been synchronized on save.
  35. $query = db_select('test_entity', 'te');
  36. $query->addJoin('INNER', 'field_data_' . $field_name, 'f', 'te.ftid = f.entity_id');
  37. $record = $query
  38. ->fields('te')
  39. ->fields('f')
  40. ->condition('ftid', $entity->ftid)
  41. ->execute()
  42. ->fetch();
  43. $phase = $entity->is_new ? 'insert' : 'update';
  44. $this->assertIdentical($record->{$label_key}, $record->{$field_name . '_value'}, t('Field synchronization is correctly performed on %phase.', array('%phase' => $phase)));
  45. unset($entity->is_new);
  46. }
  47. // Store a dummy value in the legacy field.
  48. while (($label = $this->randomName()) == $entity->{$label_key});
  49. db_update('test_entity')
  50. ->fields(array($label_key => $label))
  51. ->execute();
  52. $record = db_select('test_entity', 'te')
  53. ->fields('te')
  54. ->condition('ftid', $entity->ftid)
  55. ->execute()
  56. ->fetch();
  57. $this->assertNotIdentical($record->{$label_key}, $entity->{$label_key}, t('Entity label has been changed.'));
  58. // Clear field cache so synchronization can be performed on field attach
  59. // load.
  60. cache_clear_all('*', 'cache_field');
  61. // Check that the replacing field value is correctly synchronized on load
  62. // and view.
  63. $entity = title_test_entity_test_load($entity);
  64. title_test_phase_check('after_load', $entity);
  65. $build = entity_view('test_entity', array($entity->ftid => $entity));
  66. foreach (title_test_phase_store() as $phase => $value) {
  67. $this->assertTrue($value, t('Field synchronization is correctly performed on %phase.', array('%phase' => $phase)));
  68. }
  69. // Change the value stored into the label field to check entity_label().
  70. if (isset($info['label callback'])) {
  71. $label = $this->randomName();
  72. $entity->{$field_name}[LANGUAGE_NONE][0]['value'] = $label;
  73. $this->assertIdentical(entity_label('test_entity', $entity), $label, t('entity_label() returns the expected value.'));
  74. }
  75. }
  76. /**
  77. * Test field replacement UI.
  78. */
  79. function testFieldReplacementUI() {
  80. $admin_user = $this->drupalCreateUser(array('access administration pages', 'view the administration theme', 'administer content types', 'administer taxonomy', 'administer comments'));
  81. $this->drupalLogin($admin_user);
  82. foreach (entity_get_info() as $entity_type => $entity_info) {
  83. if (!empty($entity_info['field replacement'])) {
  84. foreach ($entity_info['bundles'] as $bundle => $bundle_info) {
  85. if (isset($bundle_info['admin']['path'])) {
  86. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle) . '/fields';
  87. foreach ($entity_info['field replacement'] as $legacy_field => $info) {
  88. $path = $admin_path . '/replace/' . $legacy_field;
  89. $xpath = '//a[@href=:url and text()=:label]';
  90. $args = array(':url' => url($path), ':label' => t('replace'));
  91. $targs = array('%legacy_field' => $legacy_field, '%entity_type' => $entity_type, '%bundle' => $bundle);
  92. $field_name = $info['field']['field_name'];
  93. // Check that the current legacy field has a "replace" operation.
  94. $this->drupalGet($admin_path);
  95. $link = $this->xpath($xpath, $args);
  96. $this->assertEqual(count($link), 1, t('Replace link found for the field %legacy_field of the bundle %bundle of the entity %entity_type.', $targs));
  97. // Check that the legacy field has correctly been replaced through
  98. // field replacement UI.
  99. $this->drupalPost($path, array('enabled' => TRUE), t('Save settings'));
  100. _field_info_collate_fields(TRUE);
  101. $link = $this->xpath($xpath, $args);
  102. $this->assertTrue(empty($link) && title_field_replacement_enabled($entity_type, $bundle, $legacy_field), t('%legacy_field successfully replaced for the bundle %bundle of the entity %entity_type.', $targs));
  103. // Check that the enabled status cannot be changed unless the
  104. // field instance is removed.
  105. $this->drupalGet($path);
  106. $this->assertFieldByXPath('//form//input[@name="enabled" and @checked="checked" and @disabled="disabled"]', NULL, t('Field replacement for %legacy_field cannot be disabled unless the replacing field instance is deleted.', array('%legacy_field' => $legacy_field)));
  107. $this->drupalPost($path, array(), t('Save settings'));
  108. _field_info_collate_fields(TRUE);
  109. $this->assertTrue(title_field_replacement_enabled($entity_type, $bundle, $legacy_field), t('Submitting the form does not alter field replacement settings.'));
  110. // Delete the field instance and check that the "replace"
  111. // operation is available again.
  112. $this->drupalPost($admin_path . '/' . $field_name . '/delete', array(), t('Delete'));
  113. $link = $this->xpath($xpath, $args);
  114. $this->assertEqual(count($link), 1, t('Replace link found for the field %legacy_field of the bundle %bundle of the entity %entity_type.', $targs));
  115. // Check that field replacement can be enabled again.
  116. $this->drupalGet($path);
  117. $this->assertFieldByXPath('//form//input[@name="enabled" and not(@checked) and not(@disabled)]', NULL, t('Field replacement for %legacy_field cannot be disabled unless the replacing field instance is deleted.', array('%legacy_field' => $legacy_field)));
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. /**
  126. * Tests for legacy field replacement.
  127. */
  128. class TitleAdminSettingsTestCase extends DrupalWebTestCase {
  129. public static function getInfo() {
  130. return array(
  131. 'name' => 'Admin settings',
  132. 'description' => 'Test the administration settings.',
  133. 'group' => 'Title',
  134. );
  135. }
  136. function setUp() {
  137. parent::setUp('field_test', 'title', 'title_test');
  138. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer taxonomy'));
  139. $this->drupalLogin($admin_user);
  140. }
  141. /**
  142. * Check for automated title_field attachment.
  143. */
  144. function testAutomatedFieldAttachement() {
  145. $this->assertAutomatedFieldAttachement(TRUE);
  146. $this->assertAutomatedFieldAttachement(FALSE);
  147. }
  148. /**
  149. * Check that the fields are replaced or skipped depdening on the given value.
  150. */
  151. function assertAutomatedFieldAttachement($enabled) {
  152. $edit = array(
  153. 'title_taxonomy_term[auto_attach][name]' => $enabled,
  154. 'title_taxonomy_term[auto_attach][description]' => $enabled,
  155. );
  156. $this->drupalPost('admin/config/content/title', $edit, t('Save configuration'));
  157. $edit = array(
  158. 'name' => $this->randomName(),
  159. 'machine_name' => drupal_strtolower($this->randomName()),
  160. 'description' => $this->randomString(16),
  161. );
  162. $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
  163. $entity_type = 'taxonomy_term';
  164. $bundle = $edit['machine_name'];
  165. field_info_cache_clear();
  166. $this->assertTrue(title_field_replacement_enabled($entity_type, $bundle, 'name') == $enabled, 'Name field correctly processed.');
  167. $this->assertTrue(title_field_replacement_enabled($entity_type, $bundle, 'description') == $enabled, 'Description field correctly processed.');
  168. }
  169. }
  170. /**
  171. * Tests for legacy field replacement.
  172. */
  173. class TitleTranslationTestCase extends DrupalWebTestCase {
  174. public static function getInfo() {
  175. return array(
  176. 'name' => 'Replaced fields translation',
  177. 'description' => 'Test replaced field translation.',
  178. 'group' => 'Title',
  179. );
  180. }
  181. function setUp() {
  182. parent::setUp('locale', 'entity_translation', 'title');
  183. // Create a power user.
  184. $admin_user = $this->drupalCreateUser(array('administer modules', 'view the administration theme', 'administer languages', 'administer taxonomy', 'administer entity translation', 'translate any entity'));
  185. $this->drupalLogin($admin_user);
  186. // Enable a translation language.
  187. $edit = array('langcode' => 'it');
  188. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  189. $this->assertTrue(drupal_multilingual(), t('Italian language installed.'));
  190. // Enable URL language negotiation.
  191. $name = 'language_content[enabled][locale-url]';
  192. $edit = array($name => 1);
  193. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  194. $this->assertFieldByName($name, 1, t('URL language negotiation enabled.'));
  195. // Enable taxonomy translation.
  196. $name = 'entity_translation_entity_types[taxonomy_term]';
  197. $edit = array($name => 1);
  198. $this->drupalPost('admin/config/regional/entity_translation', $edit, t('Save configuration'));
  199. $this->assertFieldByName($name, 'taxonomy_term', t('Taxonomy translation enabled.'));
  200. // Create a new vocabulary.
  201. $name = drupal_strtolower($this->randomName());
  202. $edit = array(
  203. 'name' => $this->randomString(),
  204. 'machine_name' => $name,
  205. );
  206. $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
  207. $this->vocabulary = taxonomy_vocabulary_machine_name_load($name);
  208. $this->assertTrue($this->vocabulary, t('Vocabulary created.'));
  209. // Replace both taxonomy term legacy fields.
  210. $entity_type = 'taxonomy_term';
  211. foreach (title_field_replacement_info($entity_type) as $legacy_field => $info) {
  212. title_field_replacement_toggle($entity_type, $name, $legacy_field);
  213. $t_args = array('%legacy_field' => $legacy_field);
  214. $this->assertTrue(field_info_instance($entity_type, $info['field']['field_name'], $name), t('The %legacy_field field has been correctly replaced.', $t_args));
  215. }
  216. }
  217. /**
  218. * Test taxonomy translation workflow.
  219. */
  220. function testTranslationWorkflow() {
  221. // Create a taxonomy term and check that legacy fields are properly
  222. // populated.
  223. $original_values = array(
  224. 'name' => $this->randomName(),
  225. 'description' => $this->randomName(),
  226. );
  227. $edit = $this->editValues($original_values, 'en');
  228. $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add', $edit, t('Save'));
  229. $term = current(entity_load('taxonomy_term', FALSE, array('name' => $original_values['name']), TRUE));
  230. $this->assertEqual($term->description, $original_values['description'], t('Taxonomy term created.'));
  231. // Translate the taxonomy term and check that both the original values and
  232. // the translations were correctly stored.
  233. $translated_values = array(
  234. 'name' => $this->randomName(),
  235. 'description' => $this->randomName(),
  236. );
  237. $edit = $this->editValues($translated_values, 'it');
  238. $this->drupalPost('it/taxonomy/term/' . $term->tid . '/edit/add/en/it', $edit, t('Save'));
  239. $term = current(entity_load('taxonomy_term', array($term->tid), array(), TRUE));
  240. $this->assertTrue($this->checkFieldValues($term, $translated_values, 'it'), t('Taxonomy term translation created.'));
  241. $this->assertTrue($this->checkFieldValues($term, $original_values, 'en'), t('Taxonomy term original values preserved.'));
  242. // Check that legacy fields have the correct values.
  243. $this->assertEqual($term->name, $original_values['name'], t('Taxonomy term name correctly stored.'));
  244. $this->assertEqual($term->description, $original_values['description'], t('Taxonomy term description correctly stored.'));
  245. // Updated the taxonomy term translation and check that both the original
  246. // values and the translations were correctly stored.
  247. $translated_values = array(
  248. 'name' => $this->randomName(),
  249. 'description' => $this->randomName(),
  250. );
  251. $edit = $this->editValues($translated_values, 'it');
  252. $this->drupalPost('it/taxonomy/term/' . $term->tid . '/edit/it', $edit, t('Save'));
  253. $term = current(entity_load('taxonomy_term', array($term->tid), array(), TRUE));
  254. $this->assertTrue($this->checkFieldValues($term, $translated_values, 'it'), t('Taxonomy term translation updated.'));
  255. $this->assertTrue($this->checkFieldValues($term, $original_values, 'en'), t('Taxonomy term original values preserved.'));
  256. // Check that legacy fields have the correct values.
  257. $this->assertEqual($term->name, $original_values['name'], t('Taxonomy term name correctly stored.'));
  258. $this->assertEqual($term->description, $original_values['description'], t('Taxonomy term description correctly stored.'));
  259. }
  260. protected function editValues($values, $langcode) {
  261. $edit = array();
  262. foreach ($values as $key => $value) {
  263. $edit["{$key}_field[{$langcode}][0][value]"] = $value;
  264. }
  265. return $edit;
  266. }
  267. protected function checkFieldValues($term, $values, $langcode) {
  268. foreach ($values as $key => $value) {
  269. if ($term->{$key . '_field'}[$langcode][0]['value'] != $value) {
  270. return FALSE;
  271. }
  272. }
  273. return TRUE;
  274. }
  275. }