PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/file/src/Tests/PrivateFileOnTranslatedEntityTest.php

http://github.com/drupal/drupal
PHP | 125 lines | 69 code | 22 blank | 34 comment | 0 complexity | 829b0a4b801de358728b9187dc4e88f7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\file\Tests;
  3. use Drupal\file\Entity\File;
  4. use Drupal\node\Entity\Node;
  5. /**
  6. * Uploads private files to translated node and checks access.
  7. *
  8. * @group file
  9. */
  10. class PrivateFileOnTranslatedEntityTest extends FileFieldTestBase {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public static $modules = array('language', 'content_translation');
  15. /**
  16. * The name of the file field used in the test.
  17. *
  18. * @var string
  19. */
  20. protected $fieldName;
  21. /**
  22. * {@inheritdoc}
  23. */
  24. protected function setUp() {
  25. parent::setUp();
  26. // Create the "Basic page" node type.
  27. $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
  28. // Create a file field on the "Basic page" node type.
  29. $this->fieldName = strtolower($this->randomMachineName());
  30. $this->createFileField($this->fieldName, 'node', 'page', array('uri_scheme' => 'private'));
  31. // Create and log in user.
  32. $permissions = array(
  33. 'access administration pages',
  34. 'administer content translation',
  35. 'administer content types',
  36. 'administer languages',
  37. 'create content translations',
  38. 'create page content',
  39. 'edit any page content',
  40. 'translate any entity',
  41. );
  42. $admin_user = $this->drupalCreateUser($permissions);
  43. $this->drupalLogin($admin_user);
  44. // Add a second language.
  45. $edit = array();
  46. $edit['predefined_langcode'] = 'fr';
  47. $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
  48. // Enable translation for "Basic page" nodes.
  49. $edit = array(
  50. 'entity_types[node]' => 1,
  51. 'settings[node][page][translatable]' => 1,
  52. "settings[node][page][fields][$this->fieldName]" => 1,
  53. );
  54. $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
  55. \Drupal::entityManager()->clearCachedDefinitions();
  56. }
  57. /**
  58. * Tests private file fields on translated nodes.
  59. */
  60. public function testPrivateLanguageFile() {
  61. // Verify that the file field on the "Basic page" node type is translatable.
  62. $definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'page');
  63. $this->assertTrue($definitions[$this->fieldName]->isTranslatable(), 'Node file field is translatable.');
  64. // Create a default language node.
  65. $default_language_node = $this->drupalCreateNode(array('type' => 'page'));
  66. // Edit the node to upload a file.
  67. $edit = array();
  68. $name = 'files[' . $this->fieldName . '_0]';
  69. $edit[$name] = drupal_realpath($this->drupalGetTestFiles('text')[0]->uri);
  70. $this->drupalPostForm('node/' . $default_language_node->id() . '/edit', $edit, t('Save'));
  71. $last_fid_prior = $this->getLastFileId();
  72. // Languages are cached on many levels, and we need to clear those caches.
  73. $this->rebuildContainer();
  74. // Ensure the file can be downloaded.
  75. \Drupal::entityManager()->getStorage('node')->resetCache(array($default_language_node->id()));
  76. $node = Node::load($default_language_node->id());
  77. $node_file = File::load($node->{$this->fieldName}->target_id);
  78. $this->drupalGet(file_create_url($node_file->getFileUri()));
  79. $this->assertResponse(200, 'Confirmed that the file attached to the English node can be downloaded.');
  80. // Translate the node into French.
  81. $this->drupalGet('node/' . $default_language_node->id() . '/translations');
  82. $this->clickLink(t('Add'));
  83. // Remove the existing file.
  84. $this->drupalPostForm(NULL, array(), t('Remove'));
  85. // Upload a different file.
  86. $edit = array();
  87. $edit['title[0][value]'] = $this->randomMachineName();
  88. $name = 'files[' . $this->fieldName . '_0]';
  89. $edit[$name] = drupal_realpath($this->drupalGetTestFiles('text')[1]->uri);
  90. $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
  91. $last_fid = $this->getLastFileId();
  92. // Verify the translation was created.
  93. \Drupal::entityManager()->getStorage('node')->resetCache(array($default_language_node->id()));
  94. $default_language_node = Node::load($default_language_node->id());
  95. $this->assertTrue($default_language_node->hasTranslation('fr'), 'Node found in database.');
  96. $this->assertTrue($last_fid > $last_fid_prior, 'New file got saved.');
  97. // Ensure the file attached to the translated node can be downloaded.
  98. $french_node = $default_language_node->getTranslation('fr');
  99. $node_file = File::load($french_node->{$this->fieldName}->target_id);
  100. $this->drupalGet(file_create_url($node_file->getFileUri()));
  101. $this->assertResponse(200, 'Confirmed that the file attached to the French node can be downloaded.');
  102. }
  103. }