summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/file/tests/src/Kernel/FileUploadHandlerTest.php
blob: 684f1e10da4a7e246f679f9dbfbe29665e8f457e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php

declare(strict_types=1);

namespace Drupal\Tests\file\Kernel;

use Drupal\Core\Lock\LockAcquiringException;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\file\Upload\FileUploadHandler;
use Drupal\file\Upload\UploadedFileInterface;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\Validator\ConstraintViolationList;

/**
 * Tests the file upload handler.
 *
 * @group file
 */
class FileUploadHandlerTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['file', 'file_validator_test'];

  /**
   * The file upload handler under test.
   */
  protected FileUploadHandler $fileUploadHandler;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->fileUploadHandler = $this->container->get('file.upload_handler');
  }

  /**
   * Test the lock acquire exception.
   */
  public function testLockAcquireException(): void {

    $lock = $this->createMock(LockBackendInterface::class);
    $lock->expects($this->once())->method('acquire')->willReturn(FALSE);

    $fileUploadHandler = new FileUploadHandler(
      $this->container->get('file_system'),
      $this->container->get('entity_type.manager'),
      $this->container->get('stream_wrapper_manager'),
      $this->container->get('event_dispatcher'),
      $this->container->get('file.mime_type.guesser'),
      $this->container->get('current_user'),
      $this->container->get('request_stack'),
      $this->container->get('file.repository'),
      $this->container->get('file.validator'),
      $lock,
      $this->container->get('validation.basic_recursive_validator_factory'),
    );

    $file_name = $this->randomMachineName();
    $file_info = $this->createMock(UploadedFileInterface::class);
    $file_info->expects($this->once())->method('getClientOriginalName')->willReturn($file_name);
    $file_info->expects($this->once())->method('validate')->willReturn(new ConstraintViolationList());

    $this->expectException(LockAcquiringException::class);
    $this->expectExceptionMessage(sprintf('File "temporary://%s" is already locked for writing.', $file_name));

    $fileUploadHandler->handleFileUpload(uploadedFile: $file_info);
  }

}