All Data Structures Functions Variables Pages
FileModelUtilTest.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 
38  {
39  public static function setUpBeforeClass()
40  {
41  parent::setUpBeforeClass();
42  SecurityTestHelper::createSuperAdmin();
43  }
44 
45  public static function tearDownAfterClass()
46  {
47  parent::tearDownAfterClass();
48  $_FILES = null;
49  }
50 
51  public static function getDependentTestModelClassNames()
52  {
53  return array('ModelWithAttachmentTestItem');
54  }
55 
56  public function setUp()
57  {
58  parent::setUp();
59  Yii::app()->user->userModel = User::getByUsername('super');
60  }
61 
62  public function testResolveModelsHasManyFilesFromPost()
63  {
64  $fileCount = FileModel::getCount();
65  $this->assertEquals(0, $fileCount);
66  $file1 = ZurmoTestHelper::createFileModel('testNote.txt');
67  $file2 = ZurmoTestHelper::createFileModel('testNote.txt');
68  $file3 = ZurmoTestHelper::createFileModel('testNote.txt');
69 
70  $model = new ModelWithAttachmentTestItem();
71  $_POST['myTest'] = array($file1->id, $file2->id, $file3->id);
72  FileModelUtil::resolveModelsHasManyFilesFromPost($model, 'files', 'myTest');
73  $model->member = 'test';
74  $saved = $model->save();
75  $this->assertTrue($saved);
76 
77  $fileCount = FileModel::getCount();
78  $this->assertEquals(3, $fileCount);
79 
80  $modelId = $model->id;
81  $model->forget();
82  $model = ModelWithAttachmentTestItem::getById($modelId);
83  $this->assertEquals(3, $model->files->count());
84 
85  //Add a fourth file.
86  $file4 = ZurmoTestHelper::createFileModel('testNote.txt');
87  $_POST['myTest'] = array($file1->id, $file2->id, $file3->id, $file4->id);
88  FileModelUtil::resolveModelsHasManyFilesFromPost($model, 'files', 'myTest');
89  $saved = $model->save();
90  $this->assertTrue($saved);
91  $fileCount = FileModel::getCount();
92  $this->assertEquals(4, $fileCount);
93  $model->forget();
94  $model = ModelWithAttachmentTestItem::getById($modelId);
95  $this->assertEquals(4, $model->files->count());
96 
97  //Remove the 2nd file.
98  $_POST['myTest'] = array($file1->id, $file3->id, $file4->id);
99  FileModelUtil::resolveModelsHasManyFilesFromPost($model, 'files', 'myTest');
100  $saved = $model->save();
101  $this->assertTrue($saved);
102  $fileCount = FileModel::getCount();
103  $this->assertEquals(3, $fileCount);
104  $model->forget();
105  $model = ModelWithAttachmentTestItem::getById($modelId);
106  $this->assertEquals(3, $model->files->count());
107  $compareIds = array($file1->id, $file3->id, $file4->id);
108  foreach ($model->files as $fileModel)
109  {
110  $this->assertTrue(in_array($fileModel->id, $compareIds));
111  }
112  }
113 
114  public function testDifferentMimeTypes()
115  {
116  $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files');
117  $contents1 = file_get_contents($pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt');
118  $contents2 = file_get_contents($pathToFiles . DIRECTORY_SEPARATOR . 'testDocument.docx');
119  $contents3 = file_get_contents($pathToFiles . DIRECTORY_SEPARATOR . 'testImage.png');
120  $contents4 = file_get_contents($pathToFiles . DIRECTORY_SEPARATOR . 'testPDF.pdf');
121  $contents5 = file_get_contents($pathToFiles . DIRECTORY_SEPARATOR . 'testZip.zip');
122 
123  $file1 = ZurmoTestHelper::createFileModel('testNote.txt');
124  $file2 = ZurmoTestHelper::createFileModel('testDocument.docx');
125  $file3 = ZurmoTestHelper::createFileModel('testImage.png');
126  $file4 = ZurmoTestHelper::createFileModel('testPDF.pdf');
127  $file5 = ZurmoTestHelper::createFileModel('testZip.zip');
128 
129  $file1Id = $file1->id;
130  $file2Id = $file2->id;
131  $file3Id = $file3->id;
132  $file4Id = $file4->id;
133  $file5Id = $file5->id;
134 
135  $file1->forget();
136  $file2->forget();
137  $file3->forget();
138  $file4->forget();
139  $file5->forget();
140 
141  $file1 = FileModel::getById($file1Id);
142  $this->assertEquals($contents1, $file1->fileContent->content);
143  $this->assertEquals('testNote.txt', $file1->name);
144  $this->assertEquals('text/plain', $file1->type);
145  $this->assertEquals(6495, $file1->size);
146 
147  $file2 = FileModel::getById($file2Id);
148  $this->assertEquals($contents2, $file2->fileContent->content);
149  $this->assertEquals('testDocument.docx', $file2->name);
150  $this->assertEquals('application/msword', $file2->type);
151  $this->assertEquals(14166, $file2->size);
152 
153  $file3 = FileModel::getById($file3Id);
154  $this->assertEquals($contents3, $file3->fileContent->content);
155  $this->assertEquals('testImage.png', $file3->name);
156  $this->assertEquals('image/png', $file3->type);
157  $this->assertEquals(3332, $file3->size);
158 
159  $file4 = FileModel::getById($file4Id);
160  $this->assertEquals($contents4, $file4->fileContent->content);
161  $this->assertEquals('testPDF.pdf', $file4->name);
162  $this->assertEquals('application/pdf', $file4->type);
163  $this->assertEquals(81075, $file4->size);
164 
165  $file5 = FileModel::getById($file5Id);
166  $this->assertEquals($contents5, $file5->fileContent->content);
167  $this->assertEquals('testZip.zip', $file5->name);
168  $this->assertEquals('application/zip', $file5->type);
169  $this->assertEquals(3492, $file5->size);
170  }
171 
172  public function testMakeByExistingFileModelId()
173  {
174  $oldFileContentsCount = FileContent::getCount();
175  $file1 = ZurmoTestHelper::createFileModel('testNote.txt');
176  $file2 = FileModelUtil::makeByExistingFileModelId($file1->id, true);
177  $file1Content = $file1->fileContent;
178  $file2Id = $file2->id;
179  $this->assertTrue($file2 !== false);
180  $this->assertEquals($file1->name, $file2->name);
181  $this->assertEquals($file1->type, $file2->type);
182  $this->assertEquals($file1->size, $file2->size);
183  $this->assertEquals($file1Content->content, $file2->fileContent->content);
184  // because fileContent was not replicated but referenced in file2.
185  $this->assertEquals($file1Content->id, $file2->fileContent->id);
186  $this->assertEquals($oldFileContentsCount + 1, FileContent::getCount());
187 
188  $file1->delete();
190  $file2 = FileModel::getById($file2Id);
191  $this->assertEquals($file1Content->content, $file2->fileContent->content);
192  // deleting file1 should still preserve its content as its being used by file2.
193  $this->assertEquals($oldFileContentsCount + 1, FileContent::getCount());
194 
195  $file2->delete();
196  // deleting fil2 should get rid of the fileContent too.
197  $this->assertEquals($oldFileContentsCount, FileContent::getCount());
198  }
199 
200  public function testMakeByExistingFileModelIdWithoutSharedContent()
201  {
202  $oldFileContentsCount = FileContent::getCount();
203  $file1 = ZurmoTestHelper::createFileModel('testNote.txt');
204  $file2 = FileModelUtil::makeByExistingFileModelId($file1->id, false);
205  $this->assertTrue($file2 !== false);
206  $this->assertEquals($file1->name, $file2->name);
207  $this->assertEquals($file1->type, $file2->type);
208  $this->assertEquals($file1->size, $file2->size);
209  $this->assertEquals($file1->fileContent->content, $file2->fileContent->content);
210  // fileContent should be replicated as we explicitly passed false to sharedContent
211  $this->assertNotEquals($file1->fileContent->id, $file2->fileContent->id);
212  $this->assertEquals($oldFileContentsCount + 2, FileContent::getCount());
213  }
214  }
215 ?>
static getByUsername($username)
Definition: User.php:49
static forgetAll()
static resolveModelsHasManyFilesFromPost(&$model, $relationName, $postDataVariableName)
static getCount(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $where=null, $modelClassName=null, $selectDistinct=false)
static getById($id, $modelClassName=null)
static makeByExistingFileModelId($fileModelId, $sharedContent=true)
Generated on Sun May 31 2020 07:10:36