ValidatorTest.php 6.49 KB
Newer Older
1 2
<?php

3 4
namespace Drupal\Tests\file\Kernel;

5
use Drupal\file\Entity\File;
6 7

/**
8 9 10
 * Tests the functions used to validate uploaded files.
 *
 * @group file
11
 */
12
class ValidatorTest extends FileManagedUnitTestBase {
13 14

  /**
15 16 17
   * An image file.
   *
   * @var \Drupal\file\FileInterface
18 19 20 21
   */
  protected $image;

  /**
22 23
   * A file which is not an image.
   *
24 25
   * @var \Drupal\file\Entity\File
   */
26
  protected $nonImage;
27

28
  protected function setUp() {
29 30
    parent::setUp();

31
    $this->image = File::create();
32
    $this->image->setFileUri('core/misc/druplicon.png');
33 34 35
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $this->image->setFilename($file_system->basename($this->image->getFileUri()));
36

37
    $this->nonImage = File::create();
38
    $this->nonImage->setFileUri('core/assets/vendor/jquery/jquery.min.js');
39
    $this->nonImage->setFilename($file_system->basename($this->nonImage->getFileUri()));
40 41 42 43 44
  }

  /**
   * Test the file_validate_extensions() function.
   */
45
  public function testFileValidateExtensions() {
46
    $file = File::create(['filename' => 'asdf.txt']);
47
    $errors = file_validate_extensions($file, 'asdf txt pork');
48
    $this->assertEqual(count($errors), 0, 'Valid extension accepted.', 'File');
49

50
    $file->setFilename('asdf.txt');
51
    $errors = file_validate_extensions($file, 'exe png');
52
    $this->assertEqual(count($errors), 1, 'Invalid extension blocked.', 'File');
53 54 55
  }

  /**
56
   * This ensures a specific file is actually an image.
57
   */
58
  public function testFileValidateIsImage() {
59
    $this->assertTrue(file_exists($this->image->getFileUri()), 'The image being tested exists.', 'File');
60
    $errors = file_validate_is_image($this->image);
61
    $this->assertEqual(count($errors), 0, 'No error reported for our image file.', 'File');
62

63 64
    $this->assertTrue(file_exists($this->nonImage->getFileUri()), 'The non-image being tested exists.', 'File');
    $errors = file_validate_is_image($this->nonImage);
65
    $this->assertEqual(count($errors), 1, 'An error reported for our non-image file.', 'File');
66 67 68
  }

  /**
69 70 71
   * This ensures the resolution of a specific file is within bounds.
   *
   * The image will be resized if it's too large.
72
   */
73
  public function testFileValidateImageResolution() {
74
    // Non-images.
75
    $errors = file_validate_image_resolution($this->nonImage);
76
    $this->assertEqual(count($errors), 0, 'Should not get any errors for a non-image file.', 'File');
77
    $errors = file_validate_image_resolution($this->nonImage, '50x50', '100x100');
78
    $this->assertEqual(count($errors), 0, 'Do not check the resolution on non files.', 'File');
79 80 81

    // Minimum size.
    $errors = file_validate_image_resolution($this->image);
82
    $this->assertEqual(count($errors), 0, 'No errors for an image when there is no minimum or maximum resolution.', 'File');
83
    $errors = file_validate_image_resolution($this->image, 0, '200x1');
84
    $this->assertEqual(count($errors), 1, 'Got an error for an image that was not wide enough.', 'File');
85
    $errors = file_validate_image_resolution($this->image, 0, '1x200');
86
    $this->assertEqual(count($errors), 1, 'Got an error for an image that was not tall enough.', 'File');
87
    $errors = file_validate_image_resolution($this->image, 0, '200x200');
88
    $this->assertEqual(count($errors), 1, 'Small images report an error.', 'File');
89 90

    // Maximum size.
91
    if ($this->container->get('image.factory')->getToolkitId()) {
92 93
      // Copy the image so that the original doesn't get resized.
      copy('core/misc/druplicon.png', 'temporary://druplicon.png');
94
      $this->image->setFileUri('temporary://druplicon.png');
95 96

      $errors = file_validate_image_resolution($this->image, '10x5');
97
      $this->assertEqual(count($errors), 0, 'No errors should be reported when an oversized image can be scaled down.', 'File');
98

99 100 101
      $image = $this->container->get('image.factory')->get($this->image->getFileUri());
      $this->assertTrue($image->getWidth() <= 10, 'Image scaled to correct width.', 'File');
      $this->assertTrue($image->getHeight() <= 5, 'Image scaled to correct height.', 'File');
102

103 104 105 106 107 108
      // Once again, now with negative width and height to force an error.
      copy('core/misc/druplicon.png', 'temporary://druplicon.png');
      $this->image->setFileUri('temporary://druplicon.png');
      $errors = file_validate_image_resolution($this->image, '-10x-5');
      $this->assertEqual(count($errors), 1, 'An error reported for an oversized image that can not be scaled down.', 'File');

109
      \Drupal::service('file_system')->unlink('temporary://druplicon.png');
110 111 112 113
    }
    else {
      // TODO: should check that the error is returned if no toolkit is available.
      $errors = file_validate_image_resolution($this->image, '5x10');
114
      $this->assertEqual(count($errors), 1, 'Oversize images that cannot be scaled get an error.', 'File');
115 116 117 118
    }
  }

  /**
119
   * This will ensure the filename length is valid.
120
   */
121
  public function testFileValidateNameLength() {
122
    // Create a new file entity.
123
    $file = File::create();
124 125

    // Add a filename with an allowed length and test it.
126 127
    $file->setFilename(str_repeat('x', 240));
    $this->assertEqual(strlen($file->getFilename()), 240);
128
    $errors = file_validate_name_length($file);
129
    $this->assertEqual(count($errors), 0, 'No errors reported for 240 length filename.', 'File');
130 131

    // Add a filename with a length too long and test it.
132
    $file->setFilename(str_repeat('x', 241));
133
    $errors = file_validate_name_length($file);
134
    $this->assertEqual(count($errors), 1, 'An error reported for 241 length filename.', 'File');
135 136

    // Add a filename with an empty string and test it.
137
    $file->setFilename('');
138
    $errors = file_validate_name_length($file);
139
    $this->assertEqual(count($errors), 1, 'An error reported for 0 length filename.', 'File');
140 141 142 143 144
  }

  /**
   * Test file_validate_size().
   */
145
  public function testFileValidateSize() {
146
    // Create a file with a size of 1000 bytes, and quotas of only 1 byte.
147
    $file = File::create(['filesize' => 1000]);
148
    $errors = file_validate_size($file, 0, 0);
149
    $this->assertEqual(count($errors), 0, 'No limits means no errors.', 'File');
150
    $errors = file_validate_size($file, 1, 0);
151
    $this->assertEqual(count($errors), 1, 'Error for the file being over the limit.', 'File');
152
    $errors = file_validate_size($file, 0, 1);
153
    $this->assertEqual(count($errors), 1, 'Error for the user being over their limit.', 'File');
154
    $errors = file_validate_size($file, 1, 1);
155
    $this->assertEqual(count($errors), 2, 'Errors for both the file and their limit.', 'File');
156
  }
157

158
}