/crashfix_webapp/protected/tests/unit/models/DebugInfoTest.php

https://gitlab.com/quyse/crashfix · PHP · 209 lines · 138 code · 33 blank · 38 comment · 0 complexity · d995ff2810f38926cca06da715080007 MD5 · raw file

  1. <?php
  2. /**
  3. * This test case contains tests for DebugInfo model class.
  4. */
  5. class DebugInfoTest extends CDbTestCase
  6. {
  7. /**
  8. * The fixtures used by this test case.
  9. */
  10. public $fixtures=array(
  11. 'debuginfo'=>':test_debuginfo',
  12. );
  13. private $_fileName;
  14. public function setUp()
  15. {
  16. parent::setUp();
  17. $dirName = Yii::app()->getBasePath()."/data/debugInfo/CrashRptd.pdb/6dbf5b63-6c53-4cc1-a9c4-5a97a7c66cad";
  18. @mkdir($dirName, 0777, true);
  19. $fileName = $dirName."/CrashRptd.pdb";
  20. $this->_fileName = $fileName;
  21. $f = @fopen($fileName, 'w');
  22. fwrite($f, 'MZP1234567');
  23. fclose($f);
  24. }
  25. public function tearDown()
  26. {
  27. parent::tearDown();
  28. @unlink($this->_fileName);
  29. }
  30. public function testCreate()
  31. {
  32. // Login as root
  33. $model = new LoginForm('RegularLogin');
  34. $model->username = "root";
  35. $model->password = "rootpwd";
  36. $this->assertTrue($model->login());
  37. // Create temp file
  38. $tmpfile1 = tempnam(Yii::app()->getRuntimePath(), "test");
  39. $tmpfile = $tmpfile1 . '.pdb';
  40. $f = fopen($tmpfile, 'w');
  41. fwrite($f, 'MZP1234567');
  42. fclose($f);
  43. // Create new model
  44. $model = new DebugInfo('create');
  45. $model->ignoreFileAttachmentErrors = true; // This is to ignore errors
  46. $model->project_id = 1;
  47. $model->guid = 'tmp_12345678901234456745';
  48. $model->fileAttachment = new CUploadedFile(
  49. 'CrashRptd.pdb',
  50. $tmpfile,
  51. 'application/octet-stream',
  52. 5,
  53. 0
  54. );
  55. // Apply changes
  56. $saved = $model->save();
  57. $this->assertTrue($saved);
  58. // Try to save again - should fail (GUID already exists)
  59. // Create new model
  60. $model2 = new DebugInfo('create');
  61. $model2->ignoreFileAttachmentErrors = true; // This is to ignore errors
  62. $model2->project_id = 1;
  63. $model2->guid = 'tmp_12345678901234456745';
  64. $model2->fileAttachment = new CUploadedFile(
  65. 'CrashRptd.pdb',
  66. $tmpfile,
  67. 'application/octet-stream',
  68. 5,
  69. 0
  70. );
  71. // Apply changes
  72. $saved2 = $model2->save();
  73. $this->assertFalse($saved2);
  74. // Remove temp file
  75. unlink($tmpfile);
  76. unlink($tmpfile1);
  77. }
  78. public function testDelete()
  79. {
  80. // Login as root
  81. $model = new LoginForm('RegularLogin');
  82. $model->username = "root";
  83. $model->password = "rootpwd";
  84. $this->assertTrue($model->login());
  85. // Find
  86. $debugInfo = DebugInfo::model()->findByPk(1);
  87. // Delete
  88. $deleted = $debugInfo->delete();
  89. // Ensure deleted
  90. $this->assertTrue($deleted);
  91. }
  92. public function testDumpFileAttachmentContent()
  93. {
  94. // Login as root
  95. $model = new LoginForm('RegularLogin');
  96. $model->username = "root";
  97. $model->password = "rootpwd";
  98. $this->assertTrue($model->login());
  99. // Create temp file
  100. $tmpfile = tempnam(Yii::app()->getRuntimePath(), "test");
  101. // Find a debug info file
  102. $debugInfo = DebugInfo::model()->findByPk(1);
  103. $this->assertTrue($debugInfo!=null);
  104. // Dump file content
  105. $debugInfo->dumpFileAttachmentContent($tmpfile);
  106. // Ensure the file exists
  107. $this->assertTrue(file_exists($tmpfile));
  108. // Ensure file size is not 0
  109. $this->assertTrue(filesize($tmpfile)>0);
  110. // Remove temp file
  111. unlink($tmpfile);
  112. }
  113. public function testAttributeLabels()
  114. {
  115. $model = new DebugInfo();
  116. $attrLabels = $model->attributeLabels();
  117. $this->assertTrue($attrLabels['dateuploaded']=='Date Uploaded');
  118. $this->assertTrue($attrLabels['filesize']=='File Size');
  119. }
  120. public function testSimpleSearchWithoutLogin()
  121. {
  122. // Test simple search without logging in
  123. $model = new DebugInfo('search');
  124. $model->isAdvancedSearch = false;
  125. $model->filter = 'Cr';
  126. $dataProvider = $model->search();
  127. $this->assertTrue($dataProvider==Null);
  128. }
  129. public function testSimpleSearch()
  130. {
  131. // Login as root
  132. $loginForm = new LoginForm('RegularLogin');
  133. $loginForm->username = "root";
  134. $loginForm->password = "rootpwd";
  135. $this->assertTrue($loginForm->login());
  136. // Test simple search
  137. $model = new DebugInfo('search');
  138. $model->isAdvancedSearch = false;
  139. $model->filter = 'Cr';
  140. $dataProvider = $model->search();
  141. // Ensure something found
  142. $this->assertTrue(count($dataProvider->data)!=0);
  143. }
  144. public function testAdvancedSearch()
  145. {
  146. // Login as root
  147. $loginForm = new LoginForm('RegularLogin');
  148. $loginForm->username = "root";
  149. $loginForm->password = "rootpwd";
  150. $this->assertTrue($loginForm->login());
  151. // Test advanced search
  152. $model = new DebugInfo('search');
  153. $model->isAdvancedSearch = true;
  154. $model->status = DebugInfo::STATUS_PROCESSED;
  155. $model->guid = '6dbf5b63-6c53-4cc1-a9c4-5a97a7c66cad';
  156. $model->dateFrom = date("m/d/Y", mktime(0, 0, 0, 4, 14, 2012));
  157. $model->dateTo = date("m/d/Y", mktime(0, 0, 0, 4, 16, 2012));
  158. // Perform search
  159. $dataProvider = $model->search();
  160. // Ensure something found
  161. $this->assertTrue(count($dataProvider->data)!=0);
  162. }
  163. public function testGenerateDebugInfoUploadStat7()
  164. {
  165. // Create temp file for output
  166. $outFile = tempnam(Yii::app()->getRuntimePath(), "test");
  167. // Generate an image for one year debug info statistics
  168. DebugInfo::generateDebugInfoUploadStat(320, 240, 7, $outFile);
  169. // Ensure the image exists
  170. $this->assertTrue(file_exists($outFile));
  171. // Ensure image size is not 0
  172. $this->assertTrue(filesize($outFile)>0);
  173. // Delete image
  174. unlink($outFile);
  175. }
  176. }