|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\dropzonejs\Kernel; |
| 4 | + |
| 5 | +use Drupal\dropzonejs\Controller\UploadController; |
| 6 | +use Drupal\KernelTests\KernelTestBase; |
| 7 | +use Symfony\Component\HttpFoundation\File\UploadedFile; |
| 8 | +use Symfony\Component\HttpFoundation\FileBag; |
| 9 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 10 | +use Symfony\Component\HttpFoundation\Request; |
| 11 | + |
| 12 | +/** |
| 13 | + * Tests dropzoneJs upload controller. |
| 14 | + * |
| 15 | + * @group DropzoneJs |
| 16 | + */ |
| 17 | +class DropzoneJsUploadControllerTest extends KernelTestBase { |
| 18 | + |
| 19 | + /** |
| 20 | + * Temporary file (location + name). |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $tmpFile = ''; |
| 25 | + |
| 26 | + /** |
| 27 | + * Temp dir. |
| 28 | + * |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + protected $filesDir = ''; |
| 32 | + |
| 33 | + /** |
| 34 | + * Testfile prefix. |
| 35 | + * |
| 36 | + * @var string |
| 37 | + */ |
| 38 | + protected $testfilePrefix = 'dropzonejstest_'; |
| 39 | + |
| 40 | + /** |
| 41 | + * Testfile data. |
| 42 | + * |
| 43 | + * @var string |
| 44 | + */ |
| 45 | + protected $testfileData = 'DropzoneJs test file data'; |
| 46 | + |
| 47 | + /** |
| 48 | + * Modules to enable. |
| 49 | + * |
| 50 | + * @var array |
| 51 | + */ |
| 52 | + public static $modules = ['system', 'file', 'user', 'dropzonejs']; |
| 53 | + |
| 54 | + /** |
| 55 | + * {@inheritdoc} |
| 56 | + */ |
| 57 | + protected function setUp() { |
| 58 | + parent::setUp(); |
| 59 | + $this->installSchema('system', 'router'); |
| 60 | + $this->installEntitySchema('user'); |
| 61 | + |
| 62 | + $this->filesDir = $this->siteDirectory . '/files'; |
| 63 | + $config = $this->container->get('config.factory'); |
| 64 | + $config->getEditable('system.file') |
| 65 | + ->set('path.temporary', $this->filesDir) |
| 66 | + ->save(); |
| 67 | + |
| 68 | + $this->tmpFile = tempnam($this->filesDir, $this->testfilePrefix); |
| 69 | + $this->tmpFile = tempnam('', $this->testfilePrefix); |
| 70 | + file_put_contents($this->tmpFile, $this->testfileData); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Test that dropzoneJs correctly handles uploads. |
| 75 | + */ |
| 76 | + public function testDropzoneJsUploadController() { |
| 77 | + $this->container->get('router.builder')->rebuild(); |
| 78 | + |
| 79 | + $uploaded_file = new UploadedFile($this->tmpFile, "{$this->testfilePrefix}controller"); |
| 80 | + $file_bag = new FileBag(); |
| 81 | + $file_bag->set('file', $uploaded_file); |
| 82 | + |
| 83 | + $request = new Request(); |
| 84 | + $request->files = $file_bag; |
| 85 | + |
| 86 | + $upload_handler = $this->container->get('dropzonejs.upload_handler'); |
| 87 | + $controller = new UploadController($upload_handler, $request); |
| 88 | + $controller_result = $controller->handleUploads(); |
| 89 | + $this->assertTrue($controller_result instanceof JsonResponse); |
| 90 | + |
| 91 | + $result = json_decode($controller_result->getContent()); |
| 92 | + $result_file = $this->filesDir . '/' . $result->result; |
| 93 | + $this->assertTrue(file_exists($result_file)); |
| 94 | + $this->assertEquals(file_get_contents($result_file), $this->testfileData); |
| 95 | + } |
| 96 | +} |
0 commit comments