From 6bea4525a1003ba411703b78d5b8d090fb1bc710 Mon Sep 17 00:00:00 2001
From: quietone <quietone@2572884.no-reply.drupal.org>
Date: Wed, 21 Feb 2024 11:28:04 +1300
Subject: [PATCH] Issue #3385845 by shalini_jha, smustgrave:
 PrivateFileOnTranslatedEntityTest should use API to set up language

---
 .../PrivateFileOnTranslatedEntityTest.php     | 43 +++++++++++++------
 1 file changed, 30 insertions(+), 13 deletions(-)

diff --git a/core/modules/file/tests/src/Functional/PrivateFileOnTranslatedEntityTest.php b/core/modules/file/tests/src/Functional/PrivateFileOnTranslatedEntityTest.php
index 47d666452009..2ebf180392b6 100644
--- a/core/modules/file/tests/src/Functional/PrivateFileOnTranslatedEntityTest.php
+++ b/core/modules/file/tests/src/Functional/PrivateFileOnTranslatedEntityTest.php
@@ -79,11 +79,15 @@ public function testPrivateLanguageFile() {
     $default_language_node = $this->drupalCreateNode(['type' => 'page']);
 
     // Edit the node to upload a file.
-    $edit = [];
-    $name = 'files[' . $this->fieldName . '_0]';
-    $edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('text')[0]->uri);
-    $this->drupalGet('node/' . $default_language_node->id() . '/edit');
-    $this->submitForm($edit, 'Save');
+    $file = File::create(
+      [
+        'uri' => $this->drupalGetTestFiles('text')[0]->uri,
+      ]
+    );
+    $file->save();
+
+    $default_language_node->set($this->fieldName, $file->id());
+    $default_language_node->save();
     $last_fid_prior = $this->getLastFileId();
 
     // Languages are cached on many levels, and we need to clear those caches.
@@ -97,18 +101,31 @@ public function testPrivateLanguageFile() {
     $this->assertSession()->statusCodeEquals(200);
 
     // Translate the node into French.
-    $this->drupalGet('node/' . $default_language_node->id() . '/translations');
-    $this->clickLink('Add');
+    $node->addTranslation(
+      'fr', [
+        'title' => $this->randomString(),
+      ]
+    );
+    $node->save();
 
     // Remove the existing file.
-    $this->submitForm([], 'Remove');
+    $existing_file = $node->{$this->fieldName}->entity;
+    if ($existing_file) {
+      $node->set($this->fieldName, NULL);
+      $existing_file->delete();
+      $node->save();
+    }
 
     // Upload a different file.
-    $edit = [];
-    $edit['title[0][value]'] = $this->randomMachineName();
-    $name = 'files[' . $this->fieldName . '_0]';
-    $edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('text')[1]->uri);
-    $this->submitForm($edit, 'Save (this translation)');
+    $default_language_node = $node->getTranslation('fr');
+    $file = File::create(
+      [
+        'uri' => $this->drupalGetTestFiles('text')[1]->uri,
+      ]
+    );
+    $file->save();
+    $default_language_node->set($this->fieldName, $file->id());
+    $default_language_node->save();
     $last_fid = $this->getLastFileId();
 
     // Verify the translation was created.
-- 
GitLab