Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
ApalinesTest.php
1 <?php
2 /**
3  * Created by JetBrains PhpStorm.
4  * User: Andrey
5  * Date: 5/22/13
6  * Time: 11:26 AM
7  * To change this template use File | Settings | File Templates.
8  */
9 
10 class ApalinesTest extends CDbTestCase{
11 
12  public $fixtures = array(
13 
14  );
15 
16  public $sourcePath;
17  public $workPath;
18  public $contentPath;
19 
20  public $CCRunner;
21 
22  public static function setUpBeforeClass(){
23 
24  Yii::app()->getModule('apalines');
25  }
26 
27  public function setUp(){
28 
29  $this->CCRunner = new CConsoleCommandRunner();
30  $this->sourcePath = Yii::getPathOfAlias('unit.apalines.content.source');
31  $this->workPath = Yii::getPathOfAlias('unit.apalines.content.work');
32  $this->contentPath = Yii::getPathOfAlias('unit.apalines.content');
33  if (!file_exists($this->workPath)) {
34  mkdir($this->workPath);
35  chmod($this->workPath, 0777);
36  }
37  $this->clearTestRecords();
38  }
39 
40  public function clearTestRecords(){
41  $news = array(
42  '49210316212944776',
43  '4913044392',
44  '49210316212959430',
45  '49210316213015736',
46  '49210316213019836',
47  '49210316212958320',
48  '49210316213016532',
49  '49210316213005018'
50  );
51  $mmNews = array(
52  '49210316212948366',
53  '4913044328'
54  );
55  foreach ($news as $docid) {
56  $newsLine = ApalinesNews::model()->find('docid = :docid', array(':docid' => $docid));
57  if ($newsLine) {
58  $newsLine->delete();
59  }
60  foreach ($mmNews as $docid) {
61  $mmFiles = ApalinesMM::model()->find('docid = :docid', array(':docid' => $docid));
62  if($mmFiles) {
63  $mmFiles->delete();
64  }
65  }
66  }
67  }
68 
69  public function testNewsWithImage(){
70 
71  $this->moveToWork('NewsWithImage');
72  $this->processApa();
73 
74  $newsDocId = '49210316212944776';
75  $mmDocId = '49210316212948366';
76 
77  $newsLine = ApalinesNews::model()->find('docid = :docid', array(':docid' => $newsDocId));
78  $this->assertNotEmpty($newsLine);
79 
80  $newsMM = ApalinesMM::model()->find('docid = :docid', array(':docid' => $mmDocId));
81  $this->assertNotEmpty($newsMM);
82 
83  $this->assertEquals($newsMM->related_maindoc_id, $newsLine->id);
84 
85  $mmFiles = ApalinesFile::model()->findAll('parent_id = :parent_id', array(':parent_id' => $newsMM->id));
86 
87  $this->assertEquals($newsMM->type, ApalinesMM::IMAGE);
88 
89  $this->assertEquals(count($mmFiles), 2);
90 
91  $newsLine->delete();
92  $newsMM->delete();
93 
94  }
95  public function testNewsWithVideo(){
96 
97  $this->moveToWork('NewsWithVideo');
98  $this->processApa();
99 
100  $newsDocId = '4913044392';
101  $mmDocId = '4913044328';
102 
103  $newsLine = ApalinesNews::model()->find('docid = :docid', array(':docid' => $newsDocId));
104  $this->assertNotEmpty($newsLine);
105 
106  $newsMM = ApalinesMM::model()->find('docid = :docid', array(':docid' => $mmDocId));
107  $this->assertNotEmpty($newsMM);
108 
109  $this->assertEquals($newsMM->related_maindoc_id, $newsLine->id);
110 
111  $mmFiles = ApalinesFile::model()->findAll('parent_id = :parent_id', array(':parent_id' => $newsMM->id));
112 
113  $this->assertEquals($newsMM->type, ApalinesMM::VIDEO);
114 
115  $this->assertEquals(count($mmFiles), 2);
116 
117  $newsLine->delete();
118  $newsMM->delete();
119 
120  }
121  public function testNewsWithoutMM(){
122 
123  $this->moveToWork('NewsWithoutMM');
124  $this->processApa();
125 
126  $newsDocId = '49210316212959430';
127 
128  $newsLine = ApalinesNews::model()->find('docid = :docid', array(':docid' => $newsDocId));
129  $this->assertNotEmpty($newsLine);
130 
131  $newsLine->delete();
132 
133  }
134  public function testNewsWithBgNews(){
135 
136  $this->moveToWork('NewsWithBgNews');
137  $this->processApa();
138 
139  $newsDocId = '49210316213015736';
140  $bgNews = '49210316213019836';
141 
142  $newsLine = ApalinesNews::model()->find('docid = :docid', array(':docid' => $newsDocId));
143  $this->assertNotEmpty($newsLine);
144 
145  $bgNewsLine = ApalinesNews::model()->find('docid = :docid', array(':docid' => $bgNews));
146  $this->assertNotEmpty($bgNewsLine);
147 
148  $this->assertEquals($newsLine->background_news, 1);
149 
150  $this->assertEquals($newsLine->id, $bgNewsLine->related_maindoc_id);
151 
152  $newsLine->delete();
153  $bgNewsLine->delete();
154 
155  }
156  public function testNewsWithoutBgNews(){
157 
158  $this->moveToWork('NewsWithoutBgNews');
159  $this->processApa();
160 
161  $newsDocId = '49210316212958320';
162 
163  $newsLine = ApalinesNews::model()->find('docid = :docid', array(':docid' => $newsDocId));
164  $this->assertNotEmpty($newsLine);
165 
166  $this->assertEquals($newsLine->background_news, 0);
167 
168  $newsLine->delete();
169 
170  }
171 
172  public function testNewsUpdate(){
173 
174  $this->moveToWork('NewsUpdate/insert');
175  $this->processApa();
176 
177  $newsDocId = '49210316213005018';
178 
179  $newsLine = ApalinesNews::model()->find('docid = :docid', array(':docid' => $newsDocId));
180  $this->assertNotEmpty($newsLine);
181 
182  $this->assertEquals($newsLine->title, 'Initial title of the news line');
183 
184  $this->moveToWork('NewsUpdate/update');
185  $this->processApa();
186 
187  $updNewsLine = ApalinesNews::model()->find('docid = :docid', array(':docid' => $newsDocId));
188  $this->assertNotEmpty($updNewsLine);
189 
190  $this->assertEquals($updNewsLine->title, 'Updated title of the news line');
191 
192  $this->assertEquals($updNewsLine->id, $newsLine->id);
193 
194  $updNewsLine->delete();
195 
196  }
197  public function testDeleteNews(){
198 
199  $this->moveToWork('NewsDelete/insert');
200  $this->processApa();
201 
202  $newsDocId = '49210316213020304';
203 
204  $newsLine = ApalinesNews::model()->find('docid = :docid', array(':docid' => $newsDocId));
205  $this->assertNotEmpty($newsLine);
206 
207  $this->moveToWork('NewsDelete/delete');
208  $this->processApa();
209 
210  $newsLine = ApalinesNews::model()->find('docid = :docid AND deleted <> 1', array(':docid' => $newsDocId));
211 
212  $this->assertEmpty($newsLine);
213  }
214 
215  private function moveToWork($sourceFolder){
216 
217  $dir = $this->workPath;
218  self::rrmdir($dir);
219 
220  $sourcePath = $this->sourcePath . DIRECTORY_SEPARATOR .$sourceFolder;
221  if (is_dir($sourcePath)) {
222  if ($dh = opendir($sourcePath)) {
223  while (($file = readdir($dh)) !== false){
224  $filePath = $sourcePath. DIRECTORY_SEPARATOR . $file;
225  if(!is_file($filePath)){
226  continue;
227  }
228  copy($filePath, $this->workPath. DIRECTORY_SEPARATOR . $file);
229  }
230  }
231  }
232  }
233 
234  private function processApa($clear = 0){
235 
236  $commandName = 'processApa';
237  $shell = new processAPACommand($commandName, $this->CCRunner);
238  $shell->actionIndex('unit_test', $clear);
239  }
240 
241  private function setupIncron(){
242 
243  $commandName = 'processApa';
244  $shell = new processAPACommand($commandName, $this->CCRunner);
245  $shell->actionSetup();
246 
247  }
248 
249  static function rrmdir($dir) {
250  if (is_dir($dir)) {
251  $objects = scandir($dir);
252  foreach ($objects as $object) {
253  if ($object != "." && $object != "..") {
254  if (filetype($dir."/".$object) == "dir") self::rrmdir($dir."/".$object); else unlink($dir."/".$object);
255  }
256  }
257  reset($objects);
258  }
259  }
260 }