All Data Structures Functions Variables Pages
ImageFileModelUtilTest.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  $user = SecurityTestHelper::createSuperAdmin();
43  Yii::app()->user->userModel = $user;
44  }
45 
46  public function testGetUrlForGetImageFromImageFileName()
47  {
48  $filename = '1_test.jpg';
49  $this->assertContains(
50  'zurmo/imageModel/getImage?fileName=' . $filename,
51  ImageFileModelUtil::getUrlForGetImageFromImageFileName($filename)
52  );
53  $this->assertContains(
54  'zurmo/imageModel/getThumb?fileName=' . $filename,
55  ImageFileModelUtil::getUrlForGetImageFromImageFileName($filename, true)
56  );
57  }
58 
59  public function testGetImageSummary()
60  {
61  $imageFileModel = new ImageFileModel();
62  $imageFileModel->name = 'test.gif';
63  $imageFileModel->width = 100;
64  $imageFileModel->height = 300;
65  $imageFileModel->type = 'image/gif';
66  $imageFileModel->save();
67  $createdDateTime = DateTimeUtil::convertDbFormattedDateTimeToLocaleFormattedDisplay($imageFileModel->createdDateTime);
68  $expectedContent = '<div class="builder-image-details"><strong>test.gif</strong><br />0 · 100 × 300 · Created by (Unnamed) on ' .
69  $createdDateTime . '</div>';
70  $this->assertContains($expectedContent, ImageFileModelUtil::getImageSummary($imageFileModel));
71 
72  $expectedContent = $createdDateTime . ' by (Unnamed) 100 × 300';
73  $this->assertContains($expectedContent,
74  ImageFileModelUtil::getImageSummary($imageFileModel,
75  "{createdTime} by {creator} {dimensions}"));
76  }
77 
78  public function testGetImageSummaryWhenThereIsNoCachedFile()
79  {
80  Yii::app()->user->userModel = User::getByUsername('super');
81  $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files');
82  $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testImage.png';
83  $fileUploadData = ImageFileModelUtil::saveImageFromTemporaryFile($filePath, 'fileNameTest');
84  $id = $fileUploadData['id'];
85  $imageFileModel = ImageFileModel::getById($id);
86  $imageFileModel->width = null;
87  $imageFileModel->height = null;
88  $this->assertTrue($imageFileModel->save());
89  unlink(ImageFileModel::getImageCachePathByFileName($imageFileModel->getImageCacheFileName(), false));
90  ImageFileModelUtil::getImageSummary($imageFileModel);
91  }
92 
93  public function testGetImageFileNameWithDimensions()
94  {
95  $filename = 'test.png';
96  $this->assertEquals('123x321 test.png', ImageFileModelUtil::getImageFileNameWithDimensions($filename, 123, 321));
97 
98  $filename = '4512x12 test.png';
99  $this->assertEquals('123x321 test.png', ImageFileModelUtil::getImageFileNameWithDimensions($filename, 123, 321));
100  }
101 
102  public function testGetImageFromHtmlImgTag()
103  {
104  Yii::app()->user->userModel = User::getByUsername('super');
105  $expectedValue = 'Due to recent improvements in the Zurmo email template builder, you are required ' .
106  'to re-import the image from the external URL. The image currently in use is located ' .
107  'here http://testimagelink.png. You can also select from an existing image in the gallery.';
108  $returnedValue = ImageFileModelUtil::getImageFromHtmlImgTag('<img src="http://testimagelink.png">');
109  $this->assertContains($expectedValue, $returnedValue);
110 
111  $imageFileModel = new ImageFileModel();
112  $imageFileModel->name = 'test.gif';
113  $imageFileModel->width = 100;
114  $imageFileModel->height = 300;
115  $imageFileModel->type = 'image/gif';
116  $imageFileModel->size = 1234;
117  $imageFileModel->fileContent->content = '122';
118  $this->assertTrue($imageFileModel->save());
119 
120  $url = Yii::app()->createAbsoluteUrl('zurmo/imageModel/getImage',
121  array('fileName' => $imageFileModel->getImageCacheFileName()));
122  $returnedValue = ImageFileModelUtil::getImageFromHtmlImgTag('<img src="'. $url .'">');
123  $this->assertSame($imageFileModel, $returnedValue);
124  }
125 
126  public function testSaveImageFromTemporaryFile()
127  {
128  $id = ImageFileModel::getCount() + 1;
129  Yii::app()->user->userModel = User::getByUsername('super');
130  $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files');
131  $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testImage.png';
132  $fileUploadData = ImageFileModelUtil::saveImageFromTemporaryFile($filePath, 'fileNameTest');
133  $this->assertEquals ($id, $fileUploadData['id']);
134  $this->assertEquals ('fileNameTest.png', $fileUploadData['name']);
135  $this->assertContains ('<img data-url=', $fileUploadData['summary']);
136  $this->assertEquals ('3.25KB', $fileUploadData['size']);
137  $this->assertContains ("getThumb?fileName={$id}_fileNameTest.png", $fileUploadData['thumbnail_url']); // Not Coding Standard
138  $this->assertContains ("getImage?fileName={$id}_fileNameTest.png", $fileUploadData['filelink']); // Not Coding Standard
139  $this->assertContains ('javascript:parent.transferModalImageValues', $fileUploadData['insert_link']);
140  }
141  }
142 ?>
static getImageCachePathByFileName($fileName, $shouldReturnThumbnail)
static getByUsername($username)
Definition: User.php:49
static getCount(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $where=null, $modelClassName=null, $selectDistinct=false)
static getById($id, $modelClassName=null)
Generated on Fri Jun 5 2020 07:10:36