Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ModelWithAttachmentTest.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
42  {
43  public static function setUpBeforeClass()
44  {
45  parent::setUpBeforeClass();
46  SecurityTestHelper::createSuperAdmin();
47  }
48 
49  public static function tearDownAfterClass()
50  {
51  parent::tearDownAfterClass();
52  $_FILES = null;
53  }
54 
55  public static function getDependentTestModelClassNames()
56  {
57  return array('ModelWithAttachmentTestItem');
58  }
59 
60  public function setUp()
61  {
62  Yii::app()->user->userModel = User::getByUsername('super');
63  }
64 
65  public function testFileSetAndGet()
66  {
67  $fileCount = FileModel::getCount();
68  $this->assertEquals(0, $fileCount);
69 
70  $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files');
71  $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt';
72  $contents = file_get_contents($pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt');
73  $this->assertEquals(6495, strlen($contents));
74  $fileContent = new FileContent();
75  $fileContent->content = $contents;
76  $file = new FileModel();
77  $file->fileContent = $fileContent;
78  $file->name = 'testNote.txt';
79  $file->type = ZurmoFileHelper::getMimeType($pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt');
80  $file->size = filesize($filePath);
81  $saved = $file->save();
82  $this->assertTrue($saved);
83  $fileId = $file->id;
84  $file->forget();
85 
86  //Now retrieve the file and make sure the content matches.
87  $file = FileModel::getById($fileId);
88  $this->assertEquals($contents, $file->fileContent->content);
89  $this->assertEquals('testNote.txt', $file->name);
90  $this->assertEquals('text/plain', $file->type);
91  $this->assertEquals(6495, $file->size);
92 
93  //Remove the fileModel. The related fileContent should also be removed because it is OWNED by the fileModel.
94  $this->assertEquals(1, FileModel::getCount());
95  $this->assertEquals(1, FileContent::getCount());
96  $file->delete();
97  $this->assertEquals(0, FileModel::getCount());
98  $this->assertEquals(0, FileContent::getCount());
99  }
100 
101  public function testMakeFileViaUpload()
102  {
103  $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files');
104  $contents = file_get_contents($pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt');
105  $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt';
106  self::resetAndPopulateFilesArrayByFilePathAndName('test', $filePath, 'testNote.txt');
107  $uploadedFile = CUploadedFile::getInstanceByName('test');
108  $fileModel = FileModelUtil::makeByUploadedFile($uploadedFile);
109  $this->assertTrue($fileModel instanceof FileModel);
110  $fileModelId = $fileModel->id;
111  $fileModel->forget();
112 
113  //Now retrieve the file and make sure the content matches.
114  $file = FileModel::getById($fileModelId);
115  $this->assertEquals($contents, $file->fileContent->content);
116  $this->assertEquals('testNote.txt', $file->name);
117  $this->assertEquals('text/plain', $file->type);
118  $this->assertEquals(6495, $file->size);
119  $fileModel->delete();
120  $this->assertEquals(0, FileModel::getCount());
121  $this->assertEquals(0, FileContent::getCount());
122  }
123 
128  {
129  $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files');
130  $contents = file_get_contents($pathToFiles . DIRECTORY_SEPARATOR . 'testEmptyNote.txt');
131  $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testEmptyNote.txt';
132  self::resetAndPopulateFilesArrayByFilePathAndName('test', $filePath, 'testEmptyNote.txt');
133  $uploadedFile = CUploadedFile::getInstanceByName('test');
134  $fileModel = FileModelUtil::makeByUploadedFile($uploadedFile);
135  }
136 
141  {
142  $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files');
143  $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testMissingFile.txt';
144  $uploadedFile = CUploadedFile::getInstanceByName('test');
145  $fileModel = FileModelUtil::makeByUploadedFile($uploadedFile);
146  }
147 
148  public function testModelWithAttachmentTestItem()
149  {
150  $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files');
151  $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt';
152 
153  // create 2 extra file model which should persist when $model is delete, their content should also persist.
154  $pdfFile = FileModelUtil::makeByFilePathAndName($filePath, 'testPDF.pdf');
155  $pdfFileId = $pdfFile->id;
156  $pdfFileName = $pdfFile->name;
157  $pdfFileContent = $pdfFile->fileContent->content;
158  $pdfFileType = $pdfFile->type;
159  $pdfFileSize = $pdfFile->size;
160  $pdfFile->forgetAll();
161  unset($pdfFile);
162  $zipFile = FileModelUtil::makeByFilePathAndName($filePath, 'testZip.zip');
163  $zipFileId = $zipFile->id;
164  $zipFileName = $zipFile->name;
165  $zipFileContent = $zipFile->fileContent->content;
166  $zipFileType = $zipFile->type;
167  $zipFileSize = $zipFile->size;
168  $zipFile->forgetAll();
169  unset($zipFile);
170 
171  $model = new ModelWithAttachmentTestItem();
172  $fileModel = FileModelUtil::makeByFilePathAndName($filePath, 'testNote.txt');
173  $firstFileModelId = $fileModel->id;
174  $this->assertTrue($fileModel instanceof FileModel);
175  $this->assertEquals(6495, strlen($fileModel->fileContent->content));
176  $theContent = $fileModel->fileContent->content;
177  $model->files->add($fileModel);
178  $model->member = 'test';
179  $saved = $model->save();
180  $this->assertTrue($saved);
181  $modelId = $model->id;
182  $model->forget();
183 
184  $model = ModelWithAttachmentTestItem::getById($modelId);
185  $this->assertEquals($theContent, $model->files[0]->fileContent->content);
186  $this->assertEquals('testNote.txt', $model->files[0]->name);
187  $this->assertEquals('text/plain', $model->files[0]->type);
188  $this->assertEquals(6495, $model->files[0]->size);
189 
190  //Now try using CUploadedFile.
191  self::resetAndPopulateFilesArrayByFilePathAndName('aTest', $filePath, 'testNote.txt');
192  $uploadedFile = CUploadedFile::getInstanceByName('aTest');
193  $this->assertTrue($uploadedFile instanceof CUploadedFile);
194  $fileModel = FileModelUtil::makeByUploadedFile($uploadedFile);
195  $this->assertTrue($fileModel instanceof FileModel);
196  $this->assertTrue($fileModel->id != $firstFileModelId);
197  $this->assertTrue($fileModel->id > 0);
198  $model = ModelWithAttachmentTestItem::getById($modelId);
199  $model->files->add($fileModel);
200  $saved = $model->save();
201  $this->assertTrue($saved);
202  $modelId = $model->id;
203  $model->forget();
205 
206  $model = ModelWithAttachmentTestItem::getById($modelId);
207  $this->assertEquals(2, $model->files->count());
208 
209  //Delete the model and confirm the related models are removed
210  $this->assertEquals(1, ModelWithAttachmentTestitem::getCount());
211  $this->assertEquals(4, FileModel::getCount());
212  $this->assertEquals(4, FileContent::getCount());
213  $this->assertTrue($model->delete());
214  $this->assertEquals(0, ModelWithAttachmentTestitem::getCount());
215  $this->assertEquals(2, FileModel::getCount());
216  $this->assertEquals(2, FileContent::getCount());
217 
218  $pdfFile = FileModel::getById($pdfFileId);
219  $this->assertEquals($pdfFileContent, $pdfFile->fileContent->content);
220  $this->assertEquals($pdfFileName, $pdfFile->name);
221  $this->assertEquals($pdfFileType, $pdfFile->type);
222  $this->assertEquals($pdfFileSize, $pdfFile->size);
223  $zipFile = FileModel::getById($zipFileId);
224  $this->assertEquals($zipFileContent, $zipFile->fileContent->content);
225  $this->assertEquals($zipFileName, $zipFile->name);
226  $this->assertEquals($zipFileType, $zipFile->type);
227  $this->assertEquals($zipFileSize, $zipFile->size);
228  }
229 
230  public function testSavingFileContentWhenNotModified()
231  {
232  $fileContent = new FileContent();
233  $fileContent-> content = 'testContent';
234  $file = new FileModel();
235  $file->fileContent = $fileContent;
236  $file->name = 'aTestName';
237  $file->type = 'aTestType';
238  $file->size = 500;
239  $saved = $file->save();
240  $this->assertTrue($saved);
241  $fileId = $file->id;
242  $file->forget();
243 
244  //Now retrieve the file and make sure the content matches.
245  $fileModel = FileModel::getById($fileId);
246  $this->assertTrue($fileModel->save());
247  $file->forget();
248  $fileModel = FileModel::getById($fileId);
249  $this->assertEquals('testContent', $file->fileContent->content);
250  }
251  }
252 ?>
static getByUsername($username)
Definition: User.php:49
static makeByUploadedFile($uploadedFile)
static makeByFilePathAndName($filePath, $fileName)
static forgetAll()
static getMimeType($file, $magicFile=null, $checkExtension=true)
static getCount(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $where=null, $modelClassName=null, $selectDistinct=false)
static getById($id, $modelClassName=null)
Generated on Fri Jul 10 2020 07:10:37
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.