From 2082ea950a1223472cc29029df850f6a08ebf873 Mon Sep 17 00:00:00 2001 From: Ian Hill Date: Fri, 23 Dec 2011 15:37:36 +0000 Subject: [PATCH] Added unit tests to show upload working with default settings from a model --- Test/Case/Model/UploadModelTest.php | 112 ++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 Test/Case/Model/UploadModelTest.php diff --git a/Test/Case/Model/UploadModelTest.php b/Test/Case/Model/UploadModelTest.php new file mode 100644 index 00000000..fab8a82e --- /dev/null +++ b/Test/Case/Model/UploadModelTest.php @@ -0,0 +1,112 @@ + array( + 'photo' => array( + 'fields' => array( + 'type' => 'type', + 'dir' => 'dir' + ), + 'mimetypes' => array( + 'image/png', + 'image/jpeg', + 'image/gif' + ), + 'thumbnailSizes' => array( + 'thumb' => '80h' // required to trigger generation of thumbnails + ) + ) + ) + ); +} + + +class UploadTestCase extends CakeTestCase { + + + public $fixtures = array( + 'plugin.upload.upload' + ); + + + public $MockUploadBehavior = null; + + + public function setUp() { + parent::setUp(); + $this->Upload = new UploadTestModel(); + $this->MockUploadBehavior = $this->getMock('UploadBehavior', array('handleUploadedFile', 'unlink', '_createThumbnails')); + + $this->MockUploadBehavior->setup($this->Upload, $this->Upload->actsAs['Upload.Upload']); + $this->Upload->Behaviors->set('Upload', $this->MockUploadBehavior); + } + + + public function tearDown() { + unset($this->Upload, $this->MockUploadBehavior); + } + + + /** + * Tests Upload::save creates a new Upload record including + * an upload of an PNG image file using the Upload.Upload behavior + * with the default path and pathMethod (primaryKey) + */ + public function testSaveSuccessPngDefaultPathAndPathMethod() { + $next_id = (1+$this->Upload->field('id', array(), array('Upload.id' => 'DESC'))); + $destination_dir = APP . 'webroot' . DS . 'files' . DS . 'upload' . DS . 'photo' . DS . $next_id . DS; + + $Upload = array( + 'Upload' => array( + 'photo' => array( + 'name' => 'image-png.png' + , 'type' => 'image/png' + , 'tmp_name' => 'image-png-tmp.png' + , 'error' => UPLOAD_ERR_OK + , 'size' => 8123 + ) + ) + ); + + $this->MockUploadBehavior->expects($this->never()) + ->method('unlink'); + + $this->MockUploadBehavior->expects($this->once()) + ->method('handleUploadedFile') + ->with( + $this->equalTo('Upload') + , $this->equalTo('photo') + , $this->equalTo('image-png-tmp.png') + , $this->equalTo($destination_dir . 'image-png.png') + ) + ->will($this->returnValue(true)); + + $this->MockUploadBehavior->expects($this->once()) + ->method('_createThumbnails') + ->with( + $this->isInstanceOf('UploadTestModel') + , $this->equalTo('photo') + , $this->equalTo($destination_dir) + , $this->equalTo($destination_dir) + ) + ->will($this->returnValue(true)); + + $this->assertTrue(false !== $this->Upload->save($Upload)); + $this->assertSame(array(), array_keys($this->Upload->validationErrors)); + + // assert file details stored in the database + $this->assertSame('image-png.png', $this->Upload->field('photo', array('Upload.id' => $next_id))); + $this->assertSame('image/png', $this->Upload->field('type', array('Upload.id' => $next_id))); + $this->assertSame((string)$next_id, $this->Upload->field('dir', array('Upload.id' => $next_id))); + } +} \ No newline at end of file