Ejemplo n.º 1
0
 public function testResolveModelsHasManyFilesFromPost()
 {
     if (!RedBeanDatabase::isFrozen()) {
         Yii::app()->user->userModel = User::getByUsername('super');
         $fileCount = count(FileModel::getAll());
         $this->assertEquals(0, $fileCount);
         $file1 = ZurmoTestHelper::createFileModel();
         $file2 = ZurmoTestHelper::createFileModel();
         $file3 = ZurmoTestHelper::createFileModel();
         $model = new ModelWithAttachmentTestItem();
         $_POST['myTest'] = array($file1->id, $file2->id, $file3->id);
         FileModelUtil::resolveModelsHasManyFilesFromPost($model, 'files', 'myTest');
         $model->member = 'test';
         $saved = $model->save();
         $this->assertTrue($saved);
         $fileCount = count(FileModel::getAll());
         $this->assertEquals(3, $fileCount);
         $modelId = $model->id;
         $model->forget();
         $model = ModelWithAttachmentTestItem::getById($modelId);
         $this->assertEquals(3, $model->files->count());
         //Add a fourth file.
         $file4 = ZurmoTestHelper::createFileModel();
         $_POST['myTest'] = array($file1->id, $file2->id, $file3->id, $file4->id);
         FileModelUtil::resolveModelsHasManyFilesFromPost($model, 'files', 'myTest');
         $saved = $model->save();
         $this->assertTrue($saved);
         $fileCount = count(FileModel::getAll());
         $this->assertEquals(4, $fileCount);
         $model->forget();
         $model = ModelWithAttachmentTestItem::getById($modelId);
         $this->assertEquals(4, $model->files->count());
         //Remove the 2nd file.
         $_POST['myTest'] = array($file1->id, $file3->id, $file4->id);
         FileModelUtil::resolveModelsHasManyFilesFromPost($model, 'files', 'myTest');
         $saved = $model->save();
         $this->assertTrue($saved);
         $fileCount = count(FileModel::getAll());
         $this->assertEquals(3, $fileCount);
         $model->forget();
         $model = ModelWithAttachmentTestItem::getById($modelId);
         $this->assertEquals(3, $model->files->count());
         $compareIds = array($file1->id, $file3->id, $file4->id);
         foreach ($model->files as $fileModel) {
             $this->assertTrue(in_array($fileModel->id, $compareIds));
         }
     }
 }
 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);
 }
Ejemplo n.º 3
0
 /**
  * @depends testAddingComments
  */
 public function testDeleteSocialItem()
 {
     $socialItems = SocialItem::getAll();
     $this->assertEquals(1, count($socialItems));
     $comments = Comment::getAll();
     $this->assertEquals(1, count($comments));
     $fileModels = FileModel::getAll();
     $this->assertEquals(1, count($fileModels));
     foreach ($socialItems as $socialItem) {
         $socialItemId = $socialItem->id;
         $socialItem->forget();
         $socialItem = SocialItem::getById($socialItemId);
         $deleted = $socialItem->delete();
         $this->assertTrue($deleted);
     }
     $socialItems = SocialItem::getAll();
     $this->assertEquals(0, count($socialItems));
     //check that all comments are removed, since they are owned.
     $comments = Comment::getAll();
     $this->assertEquals(0, count($comments));
     $fileModels = FileModel::getAll();
     $this->assertEquals(0, count($fileModels));
 }
Ejemplo n.º 4
0
 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()));
     }
 }