fb381c4ac66c15534f1cf1c5c9dd2f4e0295ae55
[tine20] / tests / tine20 / Tinebase / NotesTest.php
1 <?php
2 /**
3  * Tine 2.0
4  * 
5  * @package     Tinebase
6  * @subpackage  Notes
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Philipp Schuele <p.schuele@metaways.de>
9  * @copyright   Copyright (c) 2007-2008 Metaways Infosystems GmbH (http://www.metaways.de)
10  *
11  */
12
13 /**
14  * Test helper
15  */
16 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'TestHelper.php';
17
18 if (!defined('PHPUnit_MAIN_METHOD')) {
19     Tinebase_NotesTest::main();
20 }
21
22 /**
23  * Test class for Tinebase_Group
24  */
25 class Tinebase_NotesTest extends PHPUnit_Framework_TestCase
26 {
27     /**
28      * unit under test (UIT)
29      * @var Tinebase_Notes
30      */
31     protected $_instance;
32
33     /**
34      * @var array test objects
35      */
36     protected $_objects = array();
37     
38     /**
39      * Runs the test methods of this class.
40      */
41     public static function main()
42     {
43         $suite  = new PHPUnit_Framework_TestSuite('Tinebase_NotesTest');
44         PHPUnit_TextUI_TestRunner::run($suite);
45     }
46     
47     /**
48      * set up tests
49      *
50      */
51     public function setUp()
52     {
53         $this->_instance = Tinebase_Notes::getInstance();
54         
55         $this->_objects['record'] = array(
56             'id'        => 1,
57             'model'     => 'Addressbook_Model_Contact',
58             'backend'    => 'Sql',
59         );
60
61         $this->_objects['contact'] = new Addressbook_Model_Contact(array(
62             'id'        => 1,
63             'n_family'  => 'phpunit notes contact'
64         ));
65         
66         $this->_objects['noteType'] = new Tinebase_Model_NoteType(array(
67             'id'            => '5001',
68             'name'          => 'phpunit note type',
69             'icon'          => '/images/oxygen/16x16/actions/document-properties.png',
70             'is_user_type'  => TRUE
71         ));
72         
73         $this->_objects['note'] = new Tinebase_Model_Note(array(
74             'id'                => 123,
75             'note_type_id'      => $this->_objects['noteType']->getId(),
76             'note'              => 'phpunit test note',    
77             'record_model'      => $this->_objects['record']['model'],
78             'record_backend'    => $this->_objects['record']['backend'],       
79             'record_id'         => $this->_objects['record']['id']
80         ));
81     }
82     
83     /**
84      * try to add a note type
85      *
86      */
87     public function testAddNoteType()
88     {
89         $this->_instance->addNoteType($this->_objects['noteType']);
90         
91         // find our note type
92         $testNoteType = $this->_instance->getNoteTypeByName($this->_objects['noteType']->name);
93                 
94         $this->assertEquals($this->_objects['noteType']->name, $testNoteType->name);
95         $this->assertEquals(1, $testNoteType->is_user_type, 'user type not set');
96     }
97     
98     /**
99      * try to add a note
100      *
101      */
102     public function testAddNote()
103     {
104         $this->_instance->addNote($this->_objects['note']);
105         
106         $note = $this->_instance->getNote($this->_objects['note']->getId());
107         
108         $this->assertEquals($this->_objects['note']->note, $note->note);
109     }
110
111     /**
112      * try to add a system note
113      *
114      */
115     public function testAddSystemNote()
116     {
117         $translate = Tinebase_Translation::getTranslation('Tinebase');
118         $translatedNoteString = $translate->_('created') . ' ' . $translate->_('by');
119         
120         $this->_instance->addSystemNote(
121             $this->_objects['contact'], 
122             Zend_Registry::get('currentAccount')->getId(), 
123             Tinebase_Model_Note::SYSTEM_NOTE_NAME_CREATED
124         );
125         
126         $filter = new Tinebase_Model_NoteFilter(array(array(
127             'field' => 'query',
128             'operator' => 'contains',
129             'value' => $translatedNoteString
130         )));
131         $notes = $this->_instance->searchNotes($filter, new Tinebase_Model_Pagination());
132         
133         $this->assertGreaterThan(0, count($notes));
134         $found = FALSE;
135         foreach ($notes as $note) {
136             if ($translatedNoteString . ' ' . Zend_Registry::get('currentAccount')->accountDisplayName == $note->note) {
137                 $found = TRUE;
138                 break;
139             }
140         }
141         $this->assertTrue($found);
142     }
143     
144     /**
145      * test search notes
146      *
147      */
148     public function testSearchNotes()
149     {
150         $filter = new Tinebase_Model_NoteFilter(array(array(
151             'field' => 'query',
152             'operator' => 'contains',
153             'value' => 'phpunit'
154         )));
155         
156         $notes = $this->_instance->searchNotes($filter, new Tinebase_Model_Pagination());
157         $notesCount = $this->_instance->searchNotesCount($filter);
158         
159         $this->assertGreaterThan(0, $notesCount);
160         foreach ($notes as $note) {
161             if ($this->_objects['note']->note === $note['note']) {
162                 $found = true;
163             }
164         }
165         $this->assertTrue($found, 'note not found in notes: ' . print_r($notes, true));
166     }
167     
168     /**
169      * test to array and resolution of account display name
170      *
171      */
172     public function testToArray()
173     {
174         $note = $this->_instance->getNote($this->_objects['note']->getId());
175         
176         $noteArray = $note->toArray();
177         //print_r($noteArray);
178         
179         $this->assertEquals(Zend_Registry::get('currentAccount')->accountDisplayName, $noteArray['created_by']);
180     }
181     
182     /**
183      * try to delete a note
184      *
185      */
186     public function testDeleteNote()
187     {
188         $this->_instance->deleteNotesOfRecord(
189             $this->_objects['record']['model'], 
190             $this->_objects['record']['backend'], 
191             $this->_objects['record']['id']
192         );
193         
194         $this->setExpectedException('Tinebase_Exception_NotFound');
195         
196         $note = $this->_instance->getNote($this->_objects['note']->getId());
197     }
198     
199     /**
200      * try to delete a note type
201      *
202      */
203     public function testDeleteNoteType()
204     {
205         $noteTypesPre = $this->_instance->getNoteTypes();
206         
207         $this->_instance->deleteNoteType($this->_objects['noteType']->getId());
208         
209         $noteTypesPost = $this->_instance->getNoteTypes();
210         
211         $this->assertLessThan(count($noteTypesPre), count($noteTypesPost));
212     }
213     
214     /**
215      * try to get notes of multiple records (adding 'changed' note first)
216      * 
217      * @return void
218      */
219     public function testGetMultipleNotes()
220     {
221         $personas = Zend_Registry::get('personas');
222         $personasContactIds = array();
223         foreach ($personas as $persona) {
224             $personasContactIds[] = $persona->contact_id;
225         }
226         $contacts = Addressbook_Controller_Contact::getInstance()->getMultiple($personasContactIds);
227         
228         // add note to contacts
229         foreach ($contacts as $contact) {
230             $this->_instance->addNote(new Tinebase_Model_Note(array(
231                 'note'          => 'very important note!',
232                 'note_type_id'  => Tinebase_Notes::getInstance()->getNoteTypes()->getFirstRecord()->getId(),
233                 'record_id'     => $contact->getId(),
234                 'record_model'  => 'Addressbook_Model_Contact',
235             )));
236         }
237         
238         $this->_instance->getMultipleNotesOfRecords($contacts);
239         foreach ($contacts as $contact) {
240             $this->assertGreaterThan(0, count($contact->notes), 'No notes found for contact ' . $contact->n_fn);
241         }
242     }
243 }