Source of file FilePondTest.php
Size: 6,719 Bytes - Last Modified: 2021-12-23T10:00:35+00:00
/var/www/docs.ssmods.com/process/src/tests/FilePondTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 | <?php namespace LeKoala\FilePond\Test; use SilverStripe\Forms\Form; use SilverStripe\Assets\File; use SilverStripe\Assets\Image; use SilverStripe\Security\Member; use SilverStripe\Dev\SapphireTest; use LeKoala\FilePond\FilePondField; use SilverStripe\Control\Controller; use SilverStripe\Core\Config\Config; use SilverStripe\Assets\Upload_Validator; use SilverStripe\Control\HTTPRequest; /** * Tests for FilePond module */ class FilePondTest extends SapphireTest { /** * Defines the fixture file to use for this test class * @var string */ protected static $fixture_file = 'FilePondTest.yml'; protected static $extra_dataobjects = array( Test_FilePondModel::class, ); public function setUp() { parent::setUp(); Upload_Validator::config()->set('default_max_file_size', '5MB'); } public function tearDown() { parent::tearDown(); } public function getPond() { $controller = Controller::curr(); $controller->config()->set('url_segment', 'test_controller'); $form = new Form($controller); $uploader = new FilePondField('TestUpload'); $uploader->setRecord($this->getTestModel()); $uploader->setForm($form); return $uploader; } public function getTestModel() { return $this->objFromFixture(Test_FilePondModel::class, 'demo'); } public function getAdminMember() { return $this->objFromFixture(Member::class, 'admin'); } public function getTempFile() { return $this->objFromFixture(File::class, 'temp'); } public function getRegularFile() { return $this->objFromFixture(File::class, 'regular'); } public function testGetMaxFileSize() { $pond = new FilePondField('TestUpload'); $this->assertEquals('5MB', $pond->getMaxFileSize()); } public function testDefaultDescription() { // uploaders without records don't have a description $pond = new FilePondField('TestUpload'); $pond->Field(); // mock call to trigger default $this->assertEmpty($pond->getDescription()); // uploaders with a record have a default description $pond = $this->getPond(); $pond->Field(); // mock call to trigger default $this->assertNotEmpty($pond->getDescription()); // we can still set our own $pond->setDescription("my description"); $this->assertEquals("my description", $pond->getDescription()); // custom images sizes can be recommended $pond = $this->getPond(); $pond->setName("Image"); $pond->Field(); // mock call to trigger default $this->assertContains("1080x1080px", $pond->getDescription()); $this->assertContains("min", strtolower($pond->getDescription())); // can set a max res $pond = $this->getPond(); $pond->setName("SmallImage"); $pond->Field(); // mock call to trigger default $this->assertContains("512x512px", $pond->getDescription()); $this->assertContains("max", strtolower($pond->getDescription())); // we don't specify extensions by default $pond = $this->getPond(); $pond->Field(); // mock call to trigger default $this->assertNotContains("extensions", (string)$pond->getDescription()); // image have default type jpg, jpeg, png $pond = $this->getPond(); $pond->setName("Image"); $pond->Field(); // mock call to trigger default $this->assertContains("extensions", $pond->getDescription()); // but we do if we have a small list $pond = $this->getPond(); $pond->setName("Image"); $pond->setAllowedExtensions(['jpg', 'jpeg']); $pond->Field(); // mock call to trigger default $this->assertContains("jpg", $pond->getDescription()); } public function testGetAcceptedFileTypes() { $pond = $this->getPond(); $pond->setAllowedExtensions(['jpg', 'jpeg']); $this->assertContains('image/jpeg', $pond->getAcceptedFileTypes()); $this->assertCount(1, $pond->getAcceptedFileTypes()); } public function testGetDefaultFolderName() { $pond = $this->getPond(); $this->assertEquals("Test_FilePondModel/TestUpload", $pond->getFolderName()); } public function testRenamePattern() { $pond = $this->getPond(); $pond->setRenamePattern("{field}_{date}.{extension}"); $filename = 'mytestfile.jpg'; $expected = 'TestUpload_' . date('Ymd') . '.jpg'; $postVars = [ $pond->getName() => [ 'name' => $filename, 'error' => 0, 'test' => true, ] ]; $opts = $pond->getServerOptions(); $request = new HTTPRequest('POST', $opts['process']['url'], [], $postVars); foreach ($opts['process']['headers'] as $k => $v) { $request->addHeader($k, $v); } $response = $pond->prepareUpload($request); $this->assertEquals($expected, $response['name']); } public function testClearTempFiles() { // create a temp file $tempFile = new File(); $tempFile->IsTemporary = 1; $tempFile->write(); $result = FilePondField::clearTemporaryUploads(); $this->assertCount(1, $result); $this->assertNotEquals($tempFile->ID, $result[0]->ID, "It should not delete a recent temporary file"); $this->assertEquals($this->getTempFile()->ID, $result[0]->ID, "It should delete an old temporary file"); } public function testCustomConfig() { $pond = $this->getPond(); $pond->addFilePondConfig('allowDrop', false); $this->assertArrayHasKey('allowDrop', $pond->getFilePondConfig()); } public function testRequirements() { FilePondField::config()->use_cdn = true; FilePondField::Requirements(); FilePondField::config()->use_cdn = false; FilePondField::Requirements(); } public function testImageSizes() { $pond = $this->getPond(); $this->assertEquals(FilePondField::DEFAULT_POSTER_WIDTH, $pond->getPosterWidth()); // If the size is smaller that poster width, we use the size as is $pond->setImageSize(10, 10); $this->assertEquals(10, $pond->getPosterWidth()); // If it's larger, keep ratio $pond->setImageSize(100, 300); $ratio = 300 / FilePondField::DEFAULT_POSTER_HEIGHT; $targetWidth = 100 / $ratio; $this->assertEquals(round($targetWidth), round($pond->getPosterWidth())); } } |