title.test 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. drupal_static_reset();
  62. // Check that the replacing field value is correctly synchronized on load
  63. // and view.
  64. $entity = title_test_entity_test_load($entity);
  65. title_test_phase_check('after_load', $entity);
  66. $build = entity_view('test_entity', array($entity->ftid => $entity));
  67. foreach (title_test_phase_store() as $phase => $value) {
  68. $this->assertTrue($value, t('Field synchronization is correctly performed on %phase.', array('%phase' => $phase)));
  69. }
  70. // Change the value stored into the label field to check entity_label().
  71. if (isset($info['label callback'])) {
  72. $label = $this->randomName();
  73. $entity->{$field_name}[LANGUAGE_NONE][0]['value'] = $label;
  74. $this->assertIdentical(entity_label('test_entity', $entity), $label, t('entity_label() returns the expected value.'));
  75. }
  76. }
  77. /**
  78. * Test field replacement UI.
  79. */
  80. function testFieldReplacementUI() {
  81. $admin_user = $this->drupalCreateUser(array('access administration pages', 'view the administration theme', 'administer content types', 'administer taxonomy', 'administer comments'));
  82. $this->drupalLogin($admin_user);
  83. foreach (entity_get_info() as $entity_type => $entity_info) {
  84. if (!empty($entity_info['field replacement'])) {
  85. foreach ($entity_info['bundles'] as $bundle => $bundle_info) {
  86. if (isset($bundle_info['admin']['path'])) {
  87. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle) . '/fields';
  88. foreach ($entity_info['field replacement'] as $legacy_field => $info) {
  89. $path = $admin_path . '/replace/' . $legacy_field;
  90. $xpath = '//a[@href=:url and text()=:label]';
  91. $args = array(':url' => url($path), ':label' => t('replace'));
  92. $targs = array('%legacy_field' => $legacy_field, '%entity_type' => $entity_type, '%bundle' => $bundle);
  93. $field_name = $info['field']['field_name'];
  94. // Check that the current legacy field has a "replace" operation.
  95. $this->drupalGet($admin_path);
  96. $link = $this->xpath($xpath, $args);
  97. $this->assertEqual(count($link), 1, t('Replace link found for the field %legacy_field of the bundle %bundle of the entity %entity_type.', $targs));
  98. // Check that the legacy field has correctly been replaced through
  99. // field replacement UI.
  100. $this->drupalPost($path, array('enabled' => TRUE), t('Save settings'));
  101. _field_info_collate_fields(TRUE);
  102. $link = $this->xpath($xpath, $args);
  103. $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));
  104. // Check that the enabled status cannot be changed unless the
  105. // field instance is removed.
  106. $this->drupalGet($path);
  107. $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)));
  108. $this->drupalPost($path, array(), t('Save settings'));
  109. _field_info_collate_fields(TRUE);
  110. $this->assertTrue(title_field_replacement_enabled($entity_type, $bundle, $legacy_field), t('Submitting the form does not alter field replacement settings.'));
  111. // Delete the field instance and check that the "replace"
  112. // operation is available again.
  113. $this->drupalPost($admin_path . '/' . $field_name . '/delete', array(), t('Delete'));
  114. $link = $this->xpath($xpath, $args);
  115. $this->assertEqual(count($link), 1, t('Replace link found for the field %legacy_field of the bundle %bundle of the entity %entity_type.', $targs));
  116. // Check that field replacement can be enabled again.
  117. $this->drupalGet($path);
  118. $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)));
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }
  126. /**
  127. * Tests for legacy field replacement.
  128. */
  129. class TitleAdminSettingsTestCase extends DrupalWebTestCase {
  130. public static function getInfo() {
  131. return array(
  132. 'name' => 'Admin settings',
  133. 'description' => 'Test the administration settings.',
  134. 'group' => 'Title',
  135. );
  136. }
  137. function setUp() {
  138. parent::setUp('field_test', 'title', 'title_test');
  139. $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer taxonomy'));
  140. $this->drupalLogin($admin_user);
  141. }
  142. /**
  143. * Check for automated title_field attachment.
  144. */
  145. function testAutomatedFieldAttachement() {
  146. $this->assertAutomatedFieldAttachement(TRUE);
  147. $this->assertAutomatedFieldAttachement(FALSE);
  148. }
  149. /**
  150. * Check that the fields are replaced or skipped depdening on the given value.
  151. */
  152. function assertAutomatedFieldAttachement($enabled) {
  153. $edit = array(
  154. 'title_taxonomy_term[auto_attach][name]' => $enabled,
  155. 'title_taxonomy_term[auto_attach][description]' => $enabled,
  156. );
  157. $this->drupalPost('admin/config/content/title', $edit, t('Save configuration'));
  158. $edit = array(
  159. 'name' => $this->randomName(),
  160. 'machine_name' => drupal_strtolower($this->randomName()),
  161. 'description' => $this->randomString(16),
  162. );
  163. $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
  164. $entity_type = 'taxonomy_term';
  165. $bundle = $edit['machine_name'];
  166. field_info_cache_clear();
  167. $this->assertTrue(title_field_replacement_enabled($entity_type, $bundle, 'name') == $enabled, 'Name field correctly processed.');
  168. $this->assertTrue(title_field_replacement_enabled($entity_type, $bundle, 'description') == $enabled, 'Description field correctly processed.');
  169. }
  170. }
  171. /**
  172. * Tests for legacy field replacement.
  173. */
  174. class TitleTranslationTestCase extends DrupalWebTestCase {
  175. public static function getInfo() {
  176. return array(
  177. 'name' => 'Replaced fields translation',
  178. 'description' => 'Test replaced field translation.',
  179. 'group' => 'Title',
  180. );
  181. }
  182. protected function setUp() {
  183. parent::setUp('locale', 'entity_translation', 'title', 'field_test', 'title_test');
  184. // Create a power user.
  185. $admin_user = $this->drupalCreateUser(array('administer modules', 'view the administration theme', 'administer languages', 'administer taxonomy', 'administer entity translation', 'translate any entity'));
  186. $this->drupalLogin($admin_user);
  187. // Enable a translation language.
  188. $edit = array('langcode' => 'it');
  189. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  190. $this->assertTrue(drupal_multilingual(), t('Italian language installed.'));
  191. // Enable URL language negotiation.
  192. $name = 'language_content[enabled][locale-url]';
  193. $edit = array($name => 1);
  194. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  195. $this->assertFieldByName($name, 1, t('URL language negotiation enabled.'));
  196. // Enable taxonomy translation.
  197. $name = 'entity_translation_entity_types[taxonomy_term]';
  198. $edit = array($name => 1);
  199. $this->drupalPost('admin/config/regional/entity_translation', $edit, t('Save configuration'));
  200. $this->assertFieldByName($name, 'taxonomy_term', t('Taxonomy translation enabled.'));
  201. // Create a new vocabulary.
  202. $name = drupal_strtolower($this->randomName());
  203. $edit = array(
  204. 'name' => $this->randomString(),
  205. 'machine_name' => $name,
  206. );
  207. $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
  208. $this->vocabulary = taxonomy_vocabulary_machine_name_load($name);
  209. $this->assertTrue($this->vocabulary, t('Vocabulary created.'));
  210. // Replace both taxonomy term legacy fields.
  211. $entity_type = 'taxonomy_term';
  212. foreach (title_field_replacement_info($entity_type) as $legacy_field => $info) {
  213. title_field_replacement_toggle($entity_type, $name, $legacy_field);
  214. $t_args = array('%legacy_field' => $legacy_field);
  215. $this->assertTrue(field_info_instance($entity_type, $info['field']['field_name'], $name), t('The %legacy_field field has been correctly replaced.', $t_args));
  216. }
  217. // Ensure static caches do not interfere with API calls.
  218. drupal_static_reset();
  219. }
  220. /**
  221. * Tests taxonomy programmatic translation workflow.
  222. */
  223. public function testProgrammaticTranslationWorkflow() {
  224. // Create a taxonomy term and assign it an original language different from
  225. // the default language.
  226. $langcode = 'it';
  227. $original_values = array(
  228. 'name' => $langcode . '_' . $this->randomName(),
  229. 'description' => $langcode . '_' . $this->randomName(),
  230. );
  231. $term = (object) ($original_values + array(
  232. 'format' => 'filtered_html',
  233. 'vocabulary_machine_name' => $this->vocabulary->machine_name,
  234. 'vid' => $this->vocabulary->vid,
  235. ));
  236. entity_translation_get_handler('taxonomy_term', $term)->setOriginalLanguage($langcode);
  237. taxonomy_term_save($term);
  238. $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  239. $term = $this->termLoad($term->tid);
  240. $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), 'Replacing field values correctly created from the legacy field values.');
  241. // Pollute synchronization cache to ensure the expected values are stored
  242. // anyway.
  243. title_entity_sync('taxonomy_term', $term, $langcode);
  244. // Create a translation using the default language.
  245. $translation_langcode = language_default()->language;
  246. $translated_values = array(
  247. 'name' => $translation_langcode . '_' . $this->randomName(),
  248. 'description' => $translation_langcode . '_' . $this->randomName(),
  249. );
  250. foreach ($translated_values as $name => $value) {
  251. $term->{$name} = $value;
  252. }
  253. $translation = array(
  254. 'language' => $translation_langcode,
  255. 'source' => $langcode,
  256. 'uid' => $this->loggedInUser->uid,
  257. 'status' => 1,
  258. 'translate' => 0,
  259. 'created' => REQUEST_TIME,
  260. 'changed' => REQUEST_TIME,
  261. );
  262. entity_translation_get_handler('taxonomy_term', $term)->setTranslation($translation);
  263. taxonomy_term_save($term);
  264. $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  265. $term = $this->termLoad($term->tid, $translation_langcode);
  266. $this->assertTrue($this->checkFieldValues($term, $translated_values, $translation_langcode), 'Replacing field translations correctly created.');
  267. $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode, FALSE), 'Replacing field original values correctly preserved.');
  268. // Delete the translation.
  269. entity_translation_get_handler('taxonomy_term', $term)->removeTranslation($translation_langcode);
  270. taxonomy_term_save($term);
  271. $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  272. $term = $this->termLoad($term->tid, $langcode);
  273. $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), 'Replacing field translations correctly deleted.');
  274. // Make the term language neutral.
  275. entity_translation_get_handler('taxonomy_term', $term)->setOriginalLanguage(LANGUAGE_NONE);
  276. foreach ($original_values as $name => $value) {
  277. $field_name = $name . '_field';
  278. $term->{$field_name}[LANGUAGE_NONE] = $term->{$field_name}[$langcode];
  279. $term->{$field_name}[$langcode] = array();
  280. }
  281. taxonomy_term_save($term);
  282. $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  283. $term = $this->termLoad($term->tid);
  284. $this->assertTrue($this->checkFieldValues($term, $original_values, LANGUAGE_NONE), 'Term original language correctly changed to the former translation language.');
  285. // Change the term language to the former translation language.
  286. entity_translation_get_handler('taxonomy_term', $term)->setOriginalLanguage($translation_langcode);
  287. foreach ($original_values as $name => $value) {
  288. $field_name = $name . '_field';
  289. $term->{$field_name}[$translation_langcode] = $term->{$field_name}[LANGUAGE_NONE];
  290. $term->{$field_name}[LANGUAGE_NONE] = array();
  291. }
  292. taxonomy_term_save($term);
  293. $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  294. $term = $this->termLoad($term->tid, $translation_langcode);
  295. $this->assertTrue($this->checkFieldValues($term, $original_values, $translation_langcode), 'Term original language correctly changed to language neutral.');
  296. // Make a replacing field untranslatable and change its value.
  297. $field_name = 'name_field';
  298. $field = field_info_field($field_name);
  299. $field['translatable'] = FALSE;
  300. field_update_field($field);
  301. $original_values['name'] = LANGUAGE_NONE . '_' . $this->randomName();
  302. $term->name = $original_values['name'];
  303. taxonomy_term_save($term);
  304. $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  305. $term = $this->termLoad($term->tid);
  306. $this->assertEqual($term->{$field_name}[LANGUAGE_NONE][0]['value'], $original_values['name'], 'Untranslatable replacing field on translatable entity correctly handled.');
  307. }
  308. /**
  309. * Tests taxonomy form translation workflow.
  310. */
  311. public function testFormTranslationWorkflow() {
  312. // Create a taxonomy term and check that legacy fields are properly
  313. // populated.
  314. $original_values = array(
  315. 'name' => $this->randomName(),
  316. 'description' => $this->randomName(),
  317. );
  318. $langcode = 'en';
  319. $edit = $this->editValues($original_values, $langcode);
  320. $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add', $edit, t('Save'));
  321. $term = current(entity_load('taxonomy_term', FALSE, array('name' => $original_values['name']), TRUE));
  322. $this->assertEqual($term->description, $original_values['description'], t('Taxonomy term created.'));
  323. // Translate the taxonomy term and check that both the original values and
  324. // the translations were correctly stored.
  325. $translated_values = array(
  326. 'name' => $this->randomName(),
  327. 'description' => $this->randomName(),
  328. );
  329. $translation_langcode = 'it';
  330. $edit = $this->editValues($translated_values, 'it');
  331. $this->drupalPost($translation_langcode . '/taxonomy/term/' . $term->tid . '/edit/add/' . $langcode . '/' . $translation_langcode, $edit, t('Save'));
  332. $term = $this->termLoad($term->tid);
  333. $this->assertTrue($this->checkFieldValues($term, $translated_values, $translation_langcode, FALSE), t('Taxonomy term translation created.'));
  334. $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), t('Taxonomy term original values preserved.'));
  335. // Check that legacy fields have the correct values.
  336. $this->assertEqual($term->name, $original_values['name'], t('Taxonomy term name correctly stored.'));
  337. $this->assertEqual($term->description, $original_values['description'], t('Taxonomy term description correctly stored.'));
  338. // Updated the taxonomy term translation and check that both the original
  339. // values and the translations were correctly stored.
  340. $translated_values = array(
  341. 'name' => $this->randomName(),
  342. 'description' => $this->randomName(),
  343. );
  344. $edit = $this->editValues($translated_values, $translation_langcode);
  345. $this->drupalPost($translation_langcode . '/taxonomy/term/' . $term->tid . '/edit/' . $translation_langcode, $edit, t('Save'));
  346. $term = $this->termLoad($term->tid);
  347. $this->assertTrue($this->checkFieldValues($term, $translated_values, $translation_langcode, FALSE), t('Taxonomy term translation updated.'));
  348. $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), t('Taxonomy term original values preserved.'));
  349. // Check that legacy fields have the correct values.
  350. $this->assertEqual($term->name, $original_values['name'], t('Taxonomy term name correctly stored.'));
  351. $this->assertEqual($term->description, $original_values['description'], t('Taxonomy term description correctly stored.'));
  352. }
  353. /**
  354. * Loads a term using the given language as active language.
  355. */
  356. protected function termLoad($tid, $langcode = NULL) {
  357. drupal_static_reset();
  358. title_active_language($langcode);
  359. return current(entity_load('taxonomy_term', array($tid), array(), TRUE));
  360. }
  361. /**
  362. * Returns the drupalPost() $edit array corresponding to the given values.
  363. */
  364. protected function editValues($values, $langcode) {
  365. $edit = array();
  366. foreach ($values as $name => $value) {
  367. $edit["{$name}_field[{$langcode}][0][value]"] = $value;
  368. }
  369. return $edit;
  370. }
  371. /**
  372. * Checks that the field values and optionally the legacy ones match the given values.
  373. */
  374. protected function checkFieldValues($term, $values, $langcode, $legacy_match = TRUE) {
  375. foreach ($values as $name => $value) {
  376. $field_value = $term->{$name . '_field'}[$langcode][0]['value'];
  377. if ($field_value != $value || ($legacy_match !== ($field_value == $term->{$name}))) {
  378. return FALSE;
  379. }
  380. }
  381. return TRUE;
  382. }
  383. /**
  384. * Checks that the legacy field values stored in the database match the given values.
  385. */
  386. protected function checkLegacyValues($term, $values) {
  387. $record = db_query('SELECT * FROM {taxonomy_term_data} t WHERE t.tid = :tid', array(':tid' => $term->tid))->fetchAssoc();
  388. foreach ($values as $name => $value) {
  389. if ($record[$name] != $value) {
  390. return FALSE;
  391. }
  392. }
  393. return TRUE;
  394. }
  395. }