public function testModelWithAttachmentTestItem() { $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files'); $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt'; // create 2 extra file model which should persist when $model is delete, their content should also persist. $pdfFile = FileModelUtil::makeByFilePathAndName($filePath, 'testPDF.pdf'); $pdfFileId = $pdfFile->id; $pdfFileName = $pdfFile->name; $pdfFileContent = $pdfFile->fileContent->content; $pdfFileType = $pdfFile->type; $pdfFileSize = $pdfFile->size; $pdfFile->forgetAll(); unset($pdfFile); $zipFile = FileModelUtil::makeByFilePathAndName($filePath, 'testZip.zip'); $zipFileId = $zipFile->id; $zipFileName = $zipFile->name; $zipFileContent = $zipFile->fileContent->content; $zipFileType = $zipFile->type; $zipFileSize = $zipFile->size; $zipFile->forgetAll(); unset($zipFile); $model = new ModelWithAttachmentTestItem(); $fileModel = FileModelUtil::makeByFilePathAndName($filePath, 'testNote.txt'); $firstFileModelId = $fileModel->id; $this->assertTrue($fileModel instanceof FileModel); $this->assertEquals(6495, strlen($fileModel->fileContent->content)); $theContent = $fileModel->fileContent->content; $model->files->add($fileModel); $model->member = 'test'; $saved = $model->save(); $this->assertTrue($saved); $modelId = $model->id; $model->forget(); $model = ModelWithAttachmentTestItem::getById($modelId); $this->assertEquals($theContent, $model->files[0]->fileContent->content); $this->assertEquals('testNote.txt', $model->files[0]->name); $this->assertEquals('text/plain', $model->files[0]->type); $this->assertEquals(6495, $model->files[0]->size); //Now try using CUploadedFile. self::resetAndPopulateFilesArrayByFilePathAndName('aTest', $filePath, 'testNote.txt'); $uploadedFile = CUploadedFile::getInstanceByName('aTest'); $this->assertTrue($uploadedFile instanceof CUploadedFile); $fileModel = FileModelUtil::makeByUploadedFile($uploadedFile); $this->assertTrue($fileModel instanceof FileModel); $this->assertTrue($fileModel->id != $firstFileModelId); $this->assertTrue($fileModel->id > 0); $model = ModelWithAttachmentTestItem::getById($modelId); $model->files->add($fileModel); $saved = $model->save(); $this->assertTrue($saved); $modelId = $model->id; $model->forget(); RedBeanModel::forgetAll(); $model = ModelWithAttachmentTestItem::getById($modelId); $this->assertEquals(2, $model->files->count()); //Delete the model and confirm the related models are removed $this->assertEquals(1, ModelWithAttachmentTestitem::getCount()); $this->assertEquals(4, FileModel::getCount()); $this->assertEquals(4, FileContent::getCount()); $this->assertTrue($model->delete()); $this->assertEquals(0, ModelWithAttachmentTestitem::getCount()); $this->assertEquals(2, FileModel::getCount()); $this->assertEquals(2, FileContent::getCount()); $pdfFile = FileModel::getById($pdfFileId); $this->assertEquals($pdfFileContent, $pdfFile->fileContent->content); $this->assertEquals($pdfFileName, $pdfFile->name); $this->assertEquals($pdfFileType, $pdfFile->type); $this->assertEquals($pdfFileSize, $pdfFile->size); $zipFile = FileModel::getById($zipFileId); $this->assertEquals($zipFileContent, $zipFile->fileContent->content); $this->assertEquals($zipFileName, $zipFile->name); $this->assertEquals($zipFileType, $zipFile->type); $this->assertEquals($zipFileSize, $zipFile->size); }
public function testFileControllerActions() { $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super'); $this->assertEquals(0, count(FileModel::getAll())); $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files'); $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt'; $contents = file_get_contents($pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt'); //upload a file $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt'; self::resetAndPopulateFilesArrayByFilePathAndName('aTest', $filePath, 'testNote.txt'); $this->resetPostArray(); $this->SetGetArray(array('filesVariableName' => 'aTest')); $content = $this->runControllerWithExitExceptionAndGetContent('zurmo/fileModel/upload'); //Confirm the file has actually been uploaded $files = FileModel::getAll(); $compareJsonString = '[{"name":"testNote.txt","type":"text\\/plain","size":"6.34KB","id":' . $files[0]->id . '}]'; $this->assertEquals($compareJsonString, $content); $fileModels = FileModel::getAll(); $this->assertEquals(1, count($fileModels)); $this->assertEquals($contents, $fileModels[0]->fileContent->content); if (!RedBeanDatabase::isFrozen()) { //add fileModel to a model. $model = new ModelWithAttachmentTestItem(); $model->member = 'test'; $model->files->add($fileModels[0]); $this->assertTrue($model->save()); $modelId = $model->id; $model->forget(); //download a file. $this->setGetArray(array('id' => $fileModels[0]->id, 'modelId' => $modelId, 'modelClassName' => 'ModelWithAttachmentTestItem')); $this->resetPostArray(); $content = $this->runControllerWithExitExceptionAndGetContent('zurmo/fileModel/download'); $compareContent = 'Testing download.'; $this->assertEquals($compareContent, $content); } //todo: test all file errors. //Test deleting a file. $this->assertEquals(1, count(FileModel::getAll())); $this->assertEquals(1, count(FileContent::getAll())); $this->setGetArray(array('id' => $fileModels[0]->id)); $this->resetPostArray(); $this->runControllerWithNoExceptionsAndGetContent('zurmo/fileModel/delete', true); //Now confirm that there are no file models or content in the system. $this->assertEquals(0, count(FileModel::getAll())); $this->assertEquals(0, count(FileContent::getAll())); //Test GlobalSearchAutoComplete $this->assertTrue(ContactsModule::loadStartingData()); $this->setGetArray(array('term' => 'something')); $this->resetPostArray(); $content = $this->runControllerWithNoExceptionsAndGetContent('zurmo/default/globalSearchAutoComplete'); $compareContent = '[{"href":"","label":"No results found","iconClass":""}'; // Not Coding Standard $this->assertTrue(strpos($content, $compareContent) !== false); }
public function testModelWithAttachmentTestItem() { if (!RedBeanDatabase::isFrozen()) { Yii::app()->user->userModel = User::getByUsername('super'); $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files'); $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt'; $model = new ModelWithAttachmentTestItem(); $fileModel = FileModelUtil::makeByFilePathAndName($filePath, 'testNote.txt'); $firstFileModelId = $fileModel->id; $this->assertTrue($fileModel instanceof FileModel); $this->assertEquals(6495, strlen($fileModel->fileContent->content)); $theContent = $fileModel->fileContent->content; $model->files->add($fileModel); $model->member = 'test'; $saved = $model->save(); $this->assertTrue($saved); $modelId = $model->id; $model->forget(); $model = ModelWithAttachmentTestItem::getById($modelId); $this->assertEquals($theContent, $model->files[0]->fileContent->content); $this->assertEquals('testNote.txt', $model->files[0]->name); $this->assertEquals('text/plain', $model->files[0]->type); $this->assertEquals(6495, $model->files[0]->size); //Now try using CUploadedFile. self::resetAndPopulateFilesArrayByFilePathAndName('aTest', $filePath, 'testNote.txt'); $uploadedFile = CUploadedFile::getInstanceByName('aTest'); $this->assertTrue($uploadedFile instanceof CUploadedFile); $fileModel = FileModelUtil::makeByUploadedFile($uploadedFile); $this->assertTrue($fileModel instanceof FileModel); $this->assertTrue($fileModel->id != $firstFileModelId); $this->assertTrue($fileModel->id > 0); $model = ModelWithAttachmentTestItem::getById($modelId); $model->files->add($fileModel); $saved = $model->save(); $this->assertTrue($saved); $modelId = $model->id; $model->forget(); $model = ModelWithAttachmentTestItem::getById($modelId); $this->assertEquals(2, $model->files->count()); //Delete the model and confirm the related models are removed $this->assertEquals(1, count(ModelWithAttachmentTestitem::getAll())); $this->assertEquals(2, count(FileModel::getAll())); $this->assertEquals(2, count(FileContent::getAll())); $model->delete(); $this->assertEquals(0, count(ModelWithAttachmentTestitem::getAll())); $this->assertEquals(0, count(FileModel::getAll())); $this->assertEquals(0, count(FileContent::getAll())); } }