Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added unit tests to show upload working with default settings from a mod... #38

Closed
wants to merge 1 commit into from

2 participants

@pdata

Basic test from a models perspective added to the Plugin. If this looks ok then as time allows I can add some more tests to show things happening with different settings, validation etc.

@josegonzalez

Any chance you can add this test to the 1.3 version? I'd like to keep them mostly in sync.

@pdata

I'll have a go, but will be after xmas now before I get a chance to work on this again if that's ok. If the general style and approach to the tests is ok by you I will also try to add some additional tests with variations on the settings.

@josegonzalez

Thats fine. Since your pull request isn't adding any additional functionality, I can wait. More tests are fine by me :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 112 additions and 0 deletions.
  1. +112 −0 Test/Case/Model/UploadModelTest.php
View
112 Test/Case/Model/UploadModelTest.php
@@ -0,0 +1,112 @@
+<?php
+App::uses('Upload.Upload', 'Model/Behavior');
+
+
+class UploadTestModel extends CakeTestModel {
+
+ public $useTable = 'uploads';
+
+
+ public $name = 'Upload';
+
+
+ public $actsAs = array(
+ 'Upload.Upload' => 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)));
+ }
+}
Something went wrong with that request. Please try again.