public
Description: Git mirror of the CMS Made Simple 2.0 rewrite
Homepage: http://cmsmadesimple.org
Clone URL: git://github.com/tedkulp/cmsmadesimple-2-0.git
Search Repo:
commit  5ff950061372f6d3c32f24674eed223a0bdff9f0
tree    c306865e8d5d7b7a05696d2d643155592c1e4db5
parent  1d4e393fe0cae8142711a915f87157611a76f101
cmsmadesimple-2-0 / test / CmsEventsSpec.php
100644 212 lines (183 sloc) 7.45 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; c-basic-offset:4; -*-
#CMS - CMS Made Simple
#(c)2004-2008 by Ted Kulp (ted@cmsmadesimple.org)
#This project's homepage is: http://cmsmadesimple.org
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#$Id$
 
include_once(dirname(dirname(__FILE__)) . '/lib/cmsms.api.php');
 
class DescribeCmsEvents extends PHPSpec_Context
{
  var $callback_test = false;
  var $originator = '';
  var $event_name = '';
 
  public function before()
  {
    CmsCache::clear();
    cms_db()->Execute('DELETE FROM ' . cms_db_prefix() . 'events WHERE originator = ?', array('TestModule'));
    cms_db()->Execute('DELETE FROM ' . cms_db_prefix() . 'event_handlers WHERE module_name = ? or tag_name = ?', array('TestModuleMethod', 'TestEventTag'));
  }
  
  public function after()
  {
    $this->before();
  }
  
  public function create_event()
  {
    CmsEventOperations::create_event('TestModule', 'TestEvent1');
  }
  
  public function get_event()
  {
    return cms_orm('CmsEvent')->find_by_module_name_and_event_name('TestModule', 'TestEvent1');
  }
  
  public function get_event_count()
  {
    return cms_orm('CmsEvent')->find_count_by_module_name_and_event_name('TestModule', 'TestEvent1');
  }
  
  public function create_event_handler_for_tag()
  {
    CmsEventOperations::add_event_handler('TestModule', 'TestEvent1', 'TestEventTag');
  }
  
  public function create_event_handler_for_module()
  {
    CmsEventOperations::add_event_handler('TestModule', 'TestEvent1', false, 'TestModuleMethod');
  }
  
  public function get_event_handler_count()
  {
    return cms_orm('CmsEventHandler')->find_count_by_module_name_or_tag_name('TestModuleMethod', 'TestEventTag');
  }
  
  public function temp_callback($originator, $event_name, &$params)
  {
    $this->originator = $originator;
    $this->event_name = $event_name;
    $this->callback_test = $this->callback_test + 1;
  }
  
  public function itShouldBeAbleToCreateAnEvent()
  {
    $this->create_event();
    $this->spec($this->get_event_count())->should->be(1);
  }
  
  public function itShouldNotAllowDuplicates()
  {
    $this->create_event();
    $this->spec($this->get_event_count())->should->be(1);
    
    $this->create_event();
    $this->spec($this->get_event_count())->should->be(1);
  }
  
  public function itShouldProperlyDeleteAnEventByObject()
  {
    $this->create_event();
    $event = $this->get_event();
    $this->spec($event != null)->should->beTrue();
    $event->delete();
    $this->spec($this->get_event_count())->should->be(0);
  }
  
  public function itShouldProperlyDeleteAnEventByOperations()
  {
    $this->create_event();
    $event = $this->get_event();
    $this->spec($event != null)->should->beTrue();
    CmsEventOperations::remove_event('TestModule', 'TestEvent1');
    $this->spec($this->get_event_count())->should->be(0);
  }
  
  public function itShouldCreateAnEventHandler()
  {
    $this->create_event();
    $this->create_event_handler_for_tag();
    $this->spec($this->get_event_handler_count())->should->be(1);
  }
  
  public function itShouldNotCreateADuplicateEventHandler()
  {
    $this->create_event();
    $this->create_event_handler_for_tag();
    $this->spec($this->get_event_handler_count())->should->be(1);
    
    $this->create_event_handler_for_tag();
    $this->spec($this->get_event_handler_count())->should->be(1);
  }
  
  public function itShouldBeAbleToDeleteHandlersByOperations()
  {
    $this->create_event();
    $this->create_event_handler_for_tag();
    $this->create_event_handler_for_module();
    $this->spec($this->get_event_handler_count())->should->be(2);
    CmsEventOperations::remove_event('TestModule', 'TestEvent1');
    $this->spec($this->get_event_handler_count())->should->be(0);
    $this->spec($this->get_event_count())->should->be(0);
  }
  
  function itShouldBeAbleToRemoveHandlers()
  {
    $this->create_event();
    $this->create_event_handler_for_tag();
    $this->create_event_handler_for_module();
    $this->spec($this->get_event_handler_count())->should->be(2);
    CmsEventOperations::remove_event_handler('TestModule', 'TestEvent1', 'TestEventTag');
    $this->spec($this->get_event_handler_count())->should->be(1);
    CmsEventOperations::remove_event_handler('TestModule', 'TestEvent1', false, 'TestModuleMethod');
    $this->spec($this->get_event_handler_count())->should->be(0);
  }
  
  public function itShouldBeAbleToRemoveAllEventHandlers()
  {
    $this->create_event();
    $this->create_event_handler_for_tag();
    $this->create_event_handler_for_module();
    $this->spec($this->get_event_handler_count())->should->be(2);
    CmsEventOperations::remove_all_event_handlers('TestModule', 'TestEvent1');
    $this->spec($this->get_event_handler_count())->should->be(0);
    $this->spec($this->get_event_count())->should->be(1);
  }
  
  public function itShouldBeAbleToListEventHandlers()
  {
    $this->create_event();
    $this->create_event_handler_for_tag();
    $this->create_event_handler_for_module();
    $list = CmsEventOperations::list_event_handlers('TestModule', 'TestEvent1');
 
    $this->spec($list)->shouldNot->beNull();
    $this->spec(count($list))->should->be(2);
    CmsEventOperations::remove_all_event_handlers('TestModule', 'TestEvent1');
    
    $list = CmsEventOperations::list_event_handlers('TestModule', 'TestEvent1');
    $this->spec($list)->shouldNot->beNull();
    $this->spec(count($list))->should->be(0);
  }
  
  public function itShouldBeAbleToListEvents()
  {
    $this->create_event();
    $this->create_event_handler_for_tag();
    $this->create_event_handler_for_module();
    $this->spec(CmsEventOperations::list_events())->should->beGreaterThan(0);
  }
  
  public function itShouldBeAbleToAddTemporaryEventHandlers()
  {
    $this->callback_test = 1;
    $this->originator = '';
    $this->event_name = '';
    $this->create_event();
    CmsEventOperations::add_temp_event_handler('TestModule', 'TestEvent1', array($this, 'temp_callback'));
    CmsEventOperations::send_event('TestModule', 'TestEvent1', array('test stuff'));
    $this->spec($this->callback_test)->should->be(2);
    $this->spec($this->originator)->should->be('TestModule');
    $this->spec($this->event_name)->should->be('TestEvent1');
    CmsEventOperations::add_temp_event_handler('TestModule', 'TestEvent1', array($this, 'temp_callback'));
    CmsEventOperations::send_event('TestModule', 'TestEvent1', array('test stuff'));
    $this->spec($this->callback_test)->should->be(4);
    $this->spec($this->originator)->should->be('TestModule');
    $this->spec($this->event_name)->should->be('TestEvent1');
    CmsEventOperations::add_temp_event_handler('TestModule', 'TestEvent1', array($this, 'temp_callback'), true);
    CmsEventOperations::send_event('TestModule', 'TestEvent1', array('test stuff'));
    $this->spec($this->callback_test)->should->be(7);
    $this->spec($this->originator)->should->be('TestModule');
    $this->spec($this->event_name)->should->be('TestEvent1');
  }
 
}
 
# vim:ts=4 sw=4 noet
?>