PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/node/lib/Drupal/node/Tests/NodeTranslationUITest.php

https://bitbucket.org/aswinvk28/smartpan-stock-drupal
PHP | 269 lines | 151 code | 39 blank | 79 comment | 4 complexity | 9ae30bdd62e9821d386483bc7cf85236 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drupal\node\Tests\NodeTranslationUITest.
  5. */
  6. namespace Drupal\node\Tests;
  7. use Drupal\Core\Entity\EntityInterface;
  8. use Drupal\content_translation\Tests\ContentTranslationUITest;
  9. /**
  10. * Tests the Node Translation UI.
  11. */
  12. class NodeTranslationUITest extends ContentTranslationUITest {
  13. /**
  14. * Modules to enable.
  15. *
  16. * @var array
  17. */
  18. public static $modules = array('block', 'language', 'content_translation', 'node', 'datetime', 'field_ui');
  19. /**
  20. * The profile to install as a basis for testing.
  21. *
  22. * @var string
  23. */
  24. protected $profile = 'standard';
  25. public static function getInfo() {
  26. return array(
  27. 'name' => 'Node translation UI',
  28. 'description' => 'Tests the node translation UI.',
  29. 'group' => 'Node',
  30. );
  31. }
  32. function setUp() {
  33. $this->entityTypeId = 'node';
  34. $this->bundle = 'article';
  35. parent::setUp();
  36. // Ensure the help message is shown even with prefixed paths.
  37. $this->drupalPlaceBlock('system_help_block', array('region' => 'content'));
  38. // Display the language selector.
  39. $this->drupalLogin($this->administrator);
  40. $edit = array('language_configuration[language_show]' => TRUE);
  41. $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
  42. $this->drupalLogin($this->translator);
  43. }
  44. /**
  45. * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::getTranslatorPermission().
  46. */
  47. protected function getTranslatorPermissions() {
  48. return array_merge(parent::getTranslatorPermissions(), array('administer nodes', "edit any $this->bundle content"));
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. protected function getEditorPermissions() {
  54. return array('administer nodes', 'create article content');
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. protected function getAdministratorPermissions() {
  60. return array_merge(parent::getAdministratorPermissions(), array('access administration pages', 'administer content types', 'administer node fields', 'access content overview', 'bypass node access', 'administer languages'));
  61. }
  62. /**
  63. * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::getNewEntityValues().
  64. */
  65. protected function getNewEntityValues($langcode) {
  66. return array('title' => array(array('value' => $this->randomName()))) + parent::getNewEntityValues($langcode);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. protected function createEntity($values, $langcode, $bundle_name = NULL) {
  72. $this->drupalLogin($this->editor);
  73. $edit = array(
  74. 'title[0][value]' => $values['title'][0]['value'],
  75. "{$this->fieldName}[0][value]" => $values[$this->fieldName][0]['value'],
  76. 'langcode' => $langcode,
  77. );
  78. $this->drupalPostForm('node/add/article', $edit,t('Save and publish'));
  79. $this->drupalLogin($this->translator);
  80. $node = $this->drupalGetNodeByTitle($values['title']);
  81. return $node->id();
  82. }
  83. /**
  84. * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::getFormSubmitAction().
  85. */
  86. protected function getFormSubmitAction(EntityInterface $entity) {
  87. if ($entity->isPublished()) {
  88. return t('Save and unpublish');
  89. }
  90. return t('Save and keep unpublished');
  91. }
  92. /**
  93. * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::assertPublishedStatus().
  94. */
  95. protected function doTestPublishedStatus() {
  96. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  97. $path = $entity->getSystemPath('edit-form');
  98. $languages = language_list();
  99. $actions = array(
  100. array(t('Save and publish'), t('Save and keep published')),
  101. array(t('Save and unpublish'), t('Save and keep unpublished')),
  102. );
  103. foreach ($actions as $index => $status_actions) {
  104. // (Un)publish the node translations and check that the translation
  105. // statuses are (un)published accordingly.
  106. foreach ($this->langcodes as $langcode) {
  107. if (!empty($status_actions)) {
  108. $action = array_shift($status_actions);
  109. }
  110. $this->drupalPostForm($path, array(), $action, array('language' => $languages[$langcode]));
  111. }
  112. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  113. foreach ($this->langcodes as $langcode) {
  114. // The node is created as unpulished thus we switch to the published
  115. // status first.
  116. $status = !$index;
  117. $this->assertEqual($status, $entity->translation[$langcode]['status'], 'The translation has been correctly unpublished.');
  118. }
  119. }
  120. }
  121. /**
  122. * Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::assertAuthoringInfo().
  123. */
  124. protected function doTestAuthoringInfo() {
  125. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  126. $path = $entity->getSystemPath('edit-form');
  127. $languages = language_list();
  128. $values = array();
  129. // Post different authoring information for each translation.
  130. foreach ($this->langcodes as $langcode) {
  131. $user = $this->drupalCreateUser();
  132. $values[$langcode] = array(
  133. 'uid' => $user->id(),
  134. 'created' => REQUEST_TIME - mt_rand(0, 1000),
  135. );
  136. $edit = array(
  137. 'name' => $user->getUsername(),
  138. 'date[date]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d'),
  139. 'date[time]' => format_date($values[$langcode]['created'], 'custom', 'H:i:s'),
  140. );
  141. $this->drupalPostForm($path, $edit, $this->getFormSubmitAction($entity), array('language' => $languages[$langcode]));
  142. }
  143. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  144. foreach ($this->langcodes as $langcode) {
  145. $this->assertEqual($entity->translation[$langcode]['uid'] == $values[$langcode]['uid'], 'Translation author correctly stored.');
  146. $this->assertEqual($entity->translation[$langcode]['created'] == $values[$langcode]['created'], 'Translation date correctly stored.');
  147. }
  148. }
  149. /**
  150. * Tests translate link on content admin page.
  151. */
  152. function testTranslateLinkContentAdminPage() {
  153. $this->drupalLogin($this->administrator);
  154. $page = $this->drupalCreateNode(array('type' => 'page'));
  155. $article = $this->drupalCreateNode(array('type' => 'article', 'langcode' => $this->langcodes[0]));
  156. // Verify translation links.
  157. $this->drupalGet('admin/content');
  158. $this->assertResponse(200);
  159. $this->assertLinkByHref('node/' . $article->id() . '/translations');
  160. $this->assertNoLinkByHref('node/' . $page->id() . '/translations');
  161. }
  162. /**
  163. * Tests field translation form.
  164. */
  165. function testFieldTranslationForm() {
  166. $this->drupalLogin($this->administrator);
  167. $article = $this->drupalCreateNode(array('type' => 'article', 'langcode' => 'en'));
  168. // Visit translation page.
  169. $this->drupalGet('node/' . $article->id() . '/translations');
  170. $this->assertRaw('Not translated');
  171. // Delete the only translatable field.
  172. field_info_field($this->entityTypeId, 'field_test_et_ui_test')->delete();
  173. // Visit translation page.
  174. $this->drupalGet('node/' . $article->id() . '/translations');
  175. $this->assertRaw('No translatable fields');
  176. }
  177. /**
  178. * Tests that no metadata is stored for a disabled bundle.
  179. */
  180. public function testDisabledBundle() {
  181. // Create a bundle that does not have translation enabled.
  182. $disabledBundle = $this->randomName();
  183. $this->drupalCreateContentType(array('type' => $disabledBundle, 'name' => $disabledBundle));
  184. // Create a node for each bundle.
  185. $enabledNode = $this->drupalCreateNode(array('type' => $this->bundle));
  186. // Make sure that only a single row was inserted into the
  187. // {content_translation} table.
  188. $rows = db_query('SELECT * FROM {content_translation}')->fetchAll();
  189. $this->assertEqual(1, count($rows));
  190. $this->assertEqual($enabledNode->id(), reset($rows)->entity_id);
  191. }
  192. /**
  193. * Tests that translations are rendered properly.
  194. */
  195. function testTranslationRendering() {
  196. $default_langcode = $this->langcodes[0];
  197. $values[$default_langcode] = $this->getNewEntityValues($default_langcode);
  198. $this->entityId = $this->createEntity($values[$default_langcode], $default_langcode);
  199. $node = \Drupal::entityManager()->getStorageController($this->entityTypeId)->load($this->entityId);
  200. $node->setPromoted(TRUE);
  201. // Create translations.
  202. foreach (array_diff($this->langcodes, array($default_langcode)) as $langcode) {
  203. $values[$langcode] = $this->getNewEntityValues($langcode);
  204. $translation = $node->addTranslation($langcode, $values[$langcode]);
  205. $translation->setPromoted(TRUE);
  206. }
  207. $node->save();
  208. // Test that the frontpage view displays the correct translations.
  209. \Drupal::moduleHandler()->install(array('views'), TRUE);
  210. $this->rebuildContainer();
  211. $this->doTestTranslations('node', $values);
  212. // Test that the node page displays the correct translations.
  213. $this->doTestTranslations('node/' . $node->id(), $values);
  214. }
  215. /**
  216. * Tests that the given path dsiplays the correct translation values.
  217. *
  218. * @param string $path
  219. * The path to be tested.
  220. * @param array $values
  221. * The translation values to be found.
  222. */
  223. protected function doTestTranslations($path, array $values) {
  224. $languages = language_list();
  225. foreach ($this->langcodes as $langcode) {
  226. $this->drupalGet($path, array('language' => $languages[$langcode]));
  227. $this->assertText($values[$langcode]['title'][0]['value'], format_string('The %langcode node translation is correctly displayed.', array('%langcode' => $langcode)));
  228. }
  229. }
  230. }