This repository was archived by the owner on Jan 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #2691681 by Primsi, slashrsm: Add tests that cover file uploading
- Loading branch information
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\dropzonejs\Kernel; | ||
|
||
use Drupal\dropzonejs\Controller\UploadController; | ||
use Drupal\KernelTests\KernelTestBase; | ||
use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
use Symfony\Component\HttpFoundation\FileBag; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* Tests dropzoneJs upload controller. | ||
* | ||
* @group DropzoneJs | ||
*/ | ||
class DropzoneJsUploadControllerTest extends KernelTestBase { | ||
|
||
/** | ||
* Temporary file (location + name). | ||
* | ||
* @var string | ||
*/ | ||
protected $tmpFile = ''; | ||
|
||
/** | ||
* Temp dir. | ||
* | ||
* @var string | ||
*/ | ||
protected $filesDir = ''; | ||
|
||
/** | ||
* Testfile prefix. | ||
* | ||
* @var string | ||
*/ | ||
protected $testfilePrefix = 'dropzonejstest_'; | ||
|
||
/** | ||
* Testfile data. | ||
* | ||
* @var string | ||
*/ | ||
protected $testfileData = 'DropzoneJs test file data'; | ||
|
||
/** | ||
* Modules to enable. | ||
* | ||
* @var array | ||
*/ | ||
public static $modules = ['system', 'file', 'user', 'dropzonejs']; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp() { | ||
parent::setUp(); | ||
$this->installSchema('system', 'router'); | ||
$this->installEntitySchema('user'); | ||
|
||
$this->filesDir = $this->siteDirectory . '/files'; | ||
$config = $this->container->get('config.factory'); | ||
$config->getEditable('system.file') | ||
->set('path.temporary', $this->filesDir) | ||
->save(); | ||
|
||
$this->tmpFile = tempnam($this->filesDir, $this->testfilePrefix); | ||
$this->tmpFile = tempnam('', $this->testfilePrefix); | ||
file_put_contents($this->tmpFile, $this->testfileData); | ||
} | ||
|
||
/** | ||
* Test that dropzoneJs correctly handles uploads. | ||
*/ | ||
public function testDropzoneJsUploadController() { | ||
$this->container->get('router.builder')->rebuild(); | ||
|
||
$uploaded_file = new UploadedFile($this->tmpFile, "{$this->testfilePrefix}controller"); | ||
$file_bag = new FileBag(); | ||
$file_bag->set('file', $uploaded_file); | ||
|
||
$request = new Request(); | ||
$request->files = $file_bag; | ||
|
||
$upload_handler = $this->container->get('dropzonejs.upload_handler'); | ||
$controller = new UploadController($upload_handler, $request); | ||
$controller_result = $controller->handleUploads(); | ||
$this->assertTrue($controller_result instanceof JsonResponse); | ||
|
||
$result = json_decode($controller_result->getContent()); | ||
$result_file = $this->filesDir . '/' . $result->result; | ||
$this->assertTrue(file_exists($result_file)); | ||
$this->assertEquals(file_get_contents($result_file), $this->testfileData); | ||
} | ||
} |