TitleFieldReplacementTestCase.test 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Tests for legacy field replacement.
  4. */
  5. class TitleFieldReplacementTestCase extends DrupalWebTestCase {
  6. /**
  7. *
  8. */
  9. public static function getInfo() {
  10. return array(
  11. 'name' => 'Field replacement',
  12. 'description' => 'Test field replacement.',
  13. 'group' => 'Title',
  14. 'dependencies' => array('entity'),
  15. );
  16. }
  17. /**
  18. * Use the barebones "testing" installation profile.
  19. */
  20. protected $profile = 'testing';
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function setUp(array $modules = array()) {
  25. // Core.
  26. $modules[] = 'comment';
  27. $modules[] = 'field_test';
  28. $modules[] = 'taxonomy';
  29. // Other dependencies.
  30. $modules[] = 'entity';
  31. // This module.
  32. $modules[] = 'title';
  33. $modules[] = 'title_test';
  34. parent::setUp($modules);
  35. }
  36. /**
  37. * Test field replacement API and workflow.
  38. */
  39. public function testFieldReplacementWorkflow() {
  40. $info = entity_get_info('test_entity');
  41. $label_key = $info['entity keys']['label'];
  42. $field_name = $label_key . '_field';
  43. // Enable field replacement for the test entity.
  44. title_field_replacement_toggle('test_entity', 'test_bundle', $label_key);
  45. $i = 0;
  46. $entity = field_test_create_stub_entity(FALSE, FALSE);
  47. while ($i++ <= 1) {
  48. // The first time the entity gets created the second time gets updated.
  49. title_test_entity_save($entity);
  50. // Check that the replacing field value has been synchronized on save.
  51. $query = db_select('test_entity', 'te');
  52. $query->addJoin('INNER', 'field_data_' . $field_name, 'f', 'te.ftid = f.entity_id');
  53. $record = $query
  54. ->fields('te')
  55. ->fields('f')
  56. ->condition('ftid', $entity->ftid)
  57. ->execute()
  58. ->fetch();
  59. $phase = $entity->is_new ? 'insert' : 'update';
  60. $this->assertIdentical($record->{$label_key}, $record->{$field_name . '_value'}, t('Field synchronization is correctly performed on %phase.', array('%phase' => $phase)));
  61. unset($entity->is_new);
  62. }
  63. // Store a dummy value in the legacy field.
  64. while (($label = $this->randomName()) == $entity->{$label_key}) {
  65. }
  66. db_update('test_entity')
  67. ->fields(array($label_key => $label))
  68. ->execute();
  69. $record = db_select('test_entity', 'te')
  70. ->fields('te')
  71. ->condition('ftid', $entity->ftid)
  72. ->execute()
  73. ->fetch();
  74. $this->assertNotIdentical($record->{$label_key}, $entity->{$label_key}, t('Entity label has been changed.'));
  75. // Clear field cache so synchronization can be performed on field attach
  76. // load.
  77. cache_clear_all('*', 'cache_field');
  78. drupal_static_reset();
  79. // Check that the replacing field value is correctly synchronized on load
  80. // and view.
  81. $entity = title_test_entity_test_load($entity);
  82. title_test_phase_check('after_load', $entity);
  83. entity_view('test_entity', array($entity->ftid => $entity));
  84. foreach (title_test_phase_store() as $phase => $value) {
  85. $this->assertTrue($value, t('Field synchronization is correctly performed on %phase.', array('%phase' => $phase)));
  86. }
  87. // Change the value stored into the label field to check entity_label().
  88. if (isset($info['label callback'])) {
  89. $label = $this->randomName();
  90. $entity->{$field_name}[LANGUAGE_NONE][0]['value'] = $label;
  91. $this->assertIdentical(entity_label('test_entity', $entity), $label, t('entity_label() returns the expected value.'));
  92. }
  93. }
  94. /**
  95. * Test field replacement UI.
  96. */
  97. public function testFieldReplacementUI() {
  98. $permissions = array(
  99. 'access administration pages',
  100. 'view the administration theme',
  101. 'administer content types',
  102. 'administer taxonomy',
  103. 'administer comments',
  104. 'administer fields',
  105. );
  106. $admin_user = $this->drupalCreateUser($permissions);
  107. $this->drupalLogin($admin_user);
  108. foreach (entity_get_info() as $entity_type => $entity_info) {
  109. if (!empty($entity_info['field replacement'])) {
  110. foreach ($entity_info['bundles'] as $bundle => $bundle_info) {
  111. if (isset($bundle_info['admin']['path'])) {
  112. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle) . '/fields';
  113. foreach ($entity_info['field replacement'] as $legacy_field => $info) {
  114. $path = $admin_path . '/replace/' . $legacy_field;
  115. $xpath = '//a[@href=:url and text()=:label]';
  116. $args = array(':url' => url($path), ':label' => t('replace'));
  117. $targs = array('%legacy_field' => $legacy_field, '%entity_type' => $entity_type, '%bundle' => $bundle);
  118. $field_name = $info['field']['field_name'];
  119. // Check that the current legacy field has a "replace" operation.
  120. $this->drupalGet($admin_path);
  121. $link = $this->xpath($xpath, $args);
  122. $this->assertEqual(count($link), 1, t('Replace link found for the field %legacy_field of the bundle %bundle of the entity %entity_type.', $targs));
  123. // Check that the legacy field has correctly been replaced through
  124. // field replacement UI.
  125. $this->drupalPost($path, array('enabled' => TRUE), t('Save settings'));
  126. _field_info_collate_fields(TRUE);
  127. $link = $this->xpath($xpath, $args);
  128. $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));
  129. // Check that the enabled status cannot be changed unless the
  130. // field instance is removed.
  131. $this->drupalGet($path);
  132. $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)));
  133. $this->drupalPost($path, array(), t('Save settings'));
  134. _field_info_collate_fields(TRUE);
  135. $this->assertTrue(title_field_replacement_enabled($entity_type, $bundle, $legacy_field), t('Submitting the form does not alter field replacement settings.'));
  136. // Delete the field instance and check that the "replace"
  137. // operation is available again.
  138. $this->drupalPost($admin_path . '/' . $field_name . '/delete', array(), t('Delete'));
  139. $link = $this->xpath($xpath, $args);
  140. $this->assertEqual(count($link), 1, t('Replace link found for the field %legacy_field of the bundle %bundle of the entity %entity_type.', $targs));
  141. // Check that field replacement can be enabled again.
  142. $this->drupalGet($path);
  143. $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)));
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }