PageRenderTime 31ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/tine20/Crm/Export/PdfTest.php

https://github.com/pschuele/Tine-2.0-Open-Source-Groupware-and-CRM
PHP | 270 lines | 175 code | 33 blank | 62 comment | 5 complexity | 7bc04bf7117c36cf895d3bd8b80ffee1 MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0 - http://www.tine20.org
  4. *
  5. * @package Crm
  6. * @subpackage Export
  7. * @license http://www.gnu.org/licenses/agpl.html
  8. * @copyright Copyright (c) 2008-2009 Metaways Infosystems GmbH (http://www.metaways.de)
  9. * @author Philipp Schuele <p.schuele@metaways.de>
  10. * @version $Id: PdfTest.php 10879 2009-10-11 19:21:50Z p.schuele@metaways.de $
  11. *
  12. */
  13. /**
  14. * Test helper
  15. */
  16. require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  17. if (!defined('PHPUnit_MAIN_METHOD')) {
  18. define('PHPUnit_MAIN_METHOD', 'Crm_Export_PdfTest::main');
  19. }
  20. /**
  21. * Test class for Tinebase_Group
  22. */
  23. class Crm_Export_PdfTest extends PHPUnit_Framework_TestCase
  24. {
  25. /**
  26. * @var array test objects
  27. */
  28. protected $objects = array();
  29. /**
  30. * Runs the test methods of this class.
  31. *
  32. * @access public
  33. * @static
  34. */
  35. public static function main()
  36. {
  37. $suite = new PHPUnit_Framework_TestSuite('Tine 2.0 Crm_Export_PdfTest');
  38. PHPUnit_TextUI_TestRunner::run($suite);
  39. }
  40. /**
  41. * Sets up the fixture.
  42. * This method is called before a test is executed.
  43. *
  44. * @access protected
  45. */
  46. protected function setUp()
  47. {
  48. $personalContainer = Tinebase_Container::getInstance()->getPersonalContainer(
  49. Zend_Registry::get('currentAccount'),
  50. 'Crm',
  51. Zend_Registry::get('currentAccount'),
  52. Tinebase_Model_Grants::GRANT_EDIT
  53. );
  54. if($personalContainer->count() === 0) {
  55. $this->testContainer = Tinebase_Container::getInstance()->addPersonalContainer(Zend_Registry::get('currentAccount')->accountId, 'Crm', 'PHPUNIT');
  56. } else {
  57. $this->testContainer = $personalContainer[0];
  58. }
  59. $this->objects['lead'] = new Crm_Model_Lead(array(
  60. 'lead_name' => 'PHPUnit',
  61. 'leadstate_id' => 1,
  62. 'leadtype_id' => 1,
  63. 'leadsource_id' => 1,
  64. 'container_id' => $this->testContainer->id,
  65. 'start' => new Tinebase_DateTime( "2007-12-12" ),
  66. 'description' => 'Lead Description',
  67. 'end' => Tinebase_DateTime::now(),
  68. 'turnover' => '200000',
  69. 'probability' => 70,
  70. 'end_scheduled' => Tinebase_DateTime::now(),
  71. ));
  72. $this->objects['leadWithLink'] = new Crm_Model_Lead(array(
  73. 'lead_name' => 'PHPUnit with contact',
  74. 'leadstate_id' => 1,
  75. 'leadtype_id' => 1,
  76. 'leadsource_id' => 1,
  77. 'container_id' => $this->testContainer->id,
  78. 'start' => new Tinebase_DateTime( "2007-12-24" ),
  79. 'description' => 'Lead Description',
  80. 'end' => Tinebase_DateTime::now(),
  81. 'turnover' => '200000',
  82. 'probability' => 50,
  83. 'end_scheduled' => Tinebase_DateTime::now(),
  84. ));
  85. $this->objects['linkedContact'] = new Addressbook_Model_Contact(array(
  86. 'adr_one_countryname' => 'DE',
  87. 'adr_one_locality' => 'Hamburg',
  88. 'adr_one_postalcode' => '24xxx',
  89. 'adr_one_region' => 'Hamburg',
  90. 'adr_one_street' => 'Pickhuben 4',
  91. 'adr_one_street2' => 'no second street',
  92. 'adr_two_countryname' => 'DE',
  93. 'adr_two_locality' => 'Hamburg',
  94. 'adr_two_postalcode' => '24xxx',
  95. 'adr_two_region' => 'Hamburg',
  96. 'adr_two_street' => 'Pickhuben 4',
  97. 'adr_two_street2' => 'no second street2',
  98. 'assistent' => 'Cornelius Weiß',
  99. 'bday' => '1975-01-02 03:04:05', // new Tinebase_DateTime???
  100. 'email' => 'unittests@tine20.org',
  101. 'email_home' => 'unittests@tine20.org',
  102. 'note' => 'Bla Bla Bla',
  103. 'role' => 'Role',
  104. 'title' => 'Title',
  105. 'url' => 'http://www.tine20.org',
  106. 'url_home' => 'http://www.tine20.com',
  107. 'n_family' => 'Kneschke',
  108. 'n_fileas' => 'Kneschke, Lars',
  109. 'n_given' => 'Lars',
  110. 'n_middle' => 'no middle name',
  111. 'n_prefix' => 'no prefix',
  112. 'n_suffix' => 'no suffix',
  113. 'org_name' => 'Metaways Infosystems GmbH',
  114. 'org_unit' => 'Tine 2.0',
  115. 'tel_assistent' => '+49TELASSISTENT',
  116. 'tel_car' => '+49TELCAR',
  117. 'tel_cell' => '+49TELCELL',
  118. 'tel_cell_private' => '+49TELCELLPRIVATE',
  119. 'tel_fax' => '+49TELFAX',
  120. 'tel_fax_home' => '+49TELFAXHOME',
  121. 'tel_home' => '+49TELHOME',
  122. 'tel_pager' => '+49TELPAGER',
  123. 'tel_work' => '+49TELWORK',
  124. ));
  125. $this->objects['linkedTask'] = new Tasks_Model_Task(array(
  126. 'summary' => 'task test',
  127. ));
  128. try {
  129. $lead = Crm_Controller_Lead::getInstance()->create($this->objects['leadWithLink']);
  130. } catch ( Exception $e ) {
  131. // already there
  132. }
  133. try {
  134. $this->objects['linkedContact'] = Addressbook_Controller_Contact::getInstance()->create($this->objects['linkedContact']);
  135. } catch ( Exception $e ) {
  136. // already there
  137. }
  138. return;
  139. }
  140. /**
  141. * Tears down the fixture
  142. * This method is called after a test is executed.
  143. *
  144. * @access protected
  145. */
  146. protected function tearDown()
  147. {
  148. // delete the db entries
  149. try {
  150. Crm_Controller_Lead::getInstance()->delete($this->objects['leadWithLink']);
  151. } catch ( Exception $e ) {
  152. // access denied ?
  153. }
  154. try {
  155. Addressbook_Controller_Contact::getInstance()->delete($this->objects['linkedContact']);
  156. } catch ( Exception $e ) {
  157. // access denied ?
  158. }
  159. }
  160. /**
  161. * try to create a pdf
  162. *
  163. */
  164. public function testLeadPdf()
  165. {
  166. $pdf = new Crm_Export_Pdf();
  167. $pdf->generate($this->objects['lead']);
  168. $pdfOutput = $pdf->render();
  169. $this->assertEquals(1, preg_match("/^%PDF-1.4/", $pdfOutput));
  170. $this->assertEquals(1, preg_match("/Lead Description/", $pdfOutput));
  171. $this->assertEquals(1, preg_match("/PHPUnit/", $pdfOutput));
  172. }
  173. /**
  174. * try to create a pdf with a linked contact
  175. *
  176. */
  177. public function testLeadPdfLinkedContact()
  178. {
  179. // create lead + contact + link
  180. $lead = Crm_Controller_Lead::getInstance()->get($this->objects['leadWithLink']->getId());
  181. $lead->relations = array(array(
  182. 'own_model' => 'Crm_Model_Lead',
  183. 'own_backend' => 'Sql',
  184. 'own_id' => $lead->getId(),
  185. 'own_degree' => Tinebase_Model_Relation::DEGREE_SIBLING,
  186. 'related_model' => 'Addressbook_Model_Contact',
  187. 'related_backend' => Addressbook_Backend_Factory::SQL,
  188. 'related_id' => $this->objects['linkedContact']->id,
  189. 'type' => 'RESPONSIBLE',
  190. ));
  191. $lead = Crm_Controller_Lead::getInstance()->update($lead);
  192. $pdf = new Crm_Export_Pdf();
  193. $pdf->generate($lead);
  194. $pdfOutput = $pdf->render();
  195. //$pdf->save("test.pdf");
  196. $this->assertEquals(1, preg_match("/^%PDF-1.4/", $pdfOutput), "no pdf generated");
  197. $this->assertEquals(1, preg_match("/Lars Kneschke/", $pdfOutput), "no contact data/fullname found");
  198. // purge all relations
  199. $backend = new Tinebase_Relation_Backend_Sql();
  200. $backend->purgeAllRelations('Crm_Model_Lead', 'Sql', $this->objects['leadWithLink']->getId());
  201. }
  202. /**
  203. * try to create a pdf with a linked task
  204. *
  205. */
  206. public function testLeadPdfLinkedTask()
  207. {
  208. // create lead + task + link
  209. $task = Tasks_Controller_Task::getInstance()->create($this->objects['linkedTask']);
  210. $lead = Crm_Controller_Lead::getInstance()->get($this->objects['leadWithLink']->getId());
  211. $lead->relations = array(array(
  212. 'own_model' => 'Crm_Model_Lead',
  213. 'own_backend' => 'Sql',
  214. 'own_id' => $lead->getId(),
  215. 'own_degree' => Tinebase_Model_Relation::DEGREE_SIBLING,
  216. 'related_model' => 'Tasks_Model_Task',
  217. 'related_backend' => Tasks_Backend_Factory::SQL,
  218. 'related_id' => $task->getId(),
  219. 'type' => 'TASK'
  220. ));
  221. $lead = Crm_Controller_Lead::getInstance()->update($lead);
  222. $pdf = new Crm_Export_Pdf();
  223. $pdf->generate($lead);
  224. $pdfOutput = $pdf->render();
  225. //$pdf->save("test.pdf");
  226. $this->assertEquals(1, preg_match("/^%PDF-1.4/", $pdfOutput), "no pdf generated");
  227. $this->assertEquals(1, preg_match("/".$task->summary."/", $pdfOutput), "no summary found");
  228. // remove
  229. Tasks_Controller_Task::getInstance()->delete($task->getId());
  230. // purge all relations
  231. $backend = new Tinebase_Relation_Backend_Sql();
  232. $backend->purgeAllRelations('Crm_Model_Lead', 'Sql', $this->objects['leadWithLink']->getId());
  233. }
  234. }
  235. if (PHPUnit_MAIN_METHOD == 'Crm_Export_PdfTest::main') {
  236. Addressbook_ControllerTest::main();
  237. }