Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
class AdminGenBrowser extends sfTestBrowser
3
{
4
  protected $_modules = array('Article'       => 'articles',
5
                              'Author'        => 'authors',
6
                              'Subscription'  => 'subscriptions',
7
                              'User'          => 'users');
8
 
9
  public function __construct()
10
  {
11
    parent::__construct();
12
    $this->setTester('doctrine', 'sfTesterDoctrine');
13
 
14
    $this->_generateAdminGenModules();
15
  }
16
 
17
  public function runTests()
18
  {
19
    $this->info('Run sfDoctrinePlugin Admin Generator Tests');
20
 
21
    $methods = get_class_methods($this);
22
    foreach ($methods as $method)
23
    {
24
      if (substr($method, 0, 5) == '_test')
25
      {
26
        $this->$method();
27
      }
28
    }
29
  }
30
 
31
  protected function _testValidSort()
32
  {
33
    $this->info('Test valid sort parameter');
34
 
35
    $this->get('/users?sort=username');
36
 
37
    $matches = 0;
38
    foreach ($this->_getQueryExecutionEvents() as $event)
39
    {
40
      if (false !== strpos($event->getQuery(), 'ORDER BY u.username asc'))
41
      {
42
        ++$matches;
43
      }
44
    }
45
 
46
    $this->test()->is($matches, 1);
47
  }
48
 
49
  protected function _testInvalidSort()
50
  {
51
    $this->info('Test invalid sort parameter');
52
 
53
    $this->get('/users?sort=INVALID');
54
 
55
    // there should be no queries that match "INVALID"
56
    foreach ($this->_getQueryExecutionEvents() as $event)
57
    {
58
      $this->test()->unlike($event->getQuery(), '/INVALID/');
59
    }
60
  }
61
 
62
  protected function _testValidSortType()
63
  {
64
    $this->info('Test valid sort_type parameter');
65
 
66
    foreach (array('asc', 'desc', 'ASC', 'DESC') as $sortType)
67
    {
68
      $this->get('/users?sort=username&sort_type='.$sortType);
69
 
70
      $matches = 0;
71
      foreach ($this->_getQueryExecutionEvents() as $event)
72
      {
73
        if (false !== strpos($event->getQuery(), 'ORDER BY u.username '.$sortType))
74
        {
75
          ++$matches;
76
        }
77
      }
78
 
79
      $this->test()->is($matches, 1);
80
    }
81
  }
82
 
83
  protected function _testInvalidSortType()
84
  {
85
    $this->info('Test invalid sort_type parameter');
86
 
87
    $this->get('/users?sort=username&sort_type=INVALID');
88
 
89
    // there should be no queries that match "INVALID"
90
    foreach ($this->_getQueryExecutionEvents() as $event)
91
    {
92
      $this->test()->unlike($event->getQuery(), '/INVALID/');
93
    }
94
  }
95
 
96
  protected function _testSanityCheck()
97
  {
98
    $this->info('Admin Generator Sanity Checks');
99
 
100
    foreach ($this->_modules as $model => $module)
101
    {
102
      $this->_runAdminGenModuleSanityCheck($model, $module);
103
    }
104
  }
105
 
106
  protected function _testAdminGenTableMethod()
107
  {
108
    $this->
109
      get('/my_articles')->
110
      with('response')->isStatusCode('200')
111
    ;
112
  }
113
 
114
  protected function _testArticleI18nEmbedded()
115
  {
116
    $this->info('Testing "articles" module embeds I18n');
117
 
118
    $info = array('author_id' => 1, 'is_on_homepage' => false, 'en' => array('title' => 'Test English title', 'body' => 'Test English body'), 'fr' => array('title' => 'Test French title', 'body' => 'Test French body'), 'created_at' => array('month' => '1', 'day' => '12', 'year' => '2009', 'hour' => '10', 'minute' => '03'), 'updated_at' => array('month' => '1', 'day' => '12', 'year' => '2009', 'hour' => '10', 'minute' => '03'));
119
 
120
    $this->
121
      get('/articles/new')->
122
        with('response')->begin()->
123
          matches('/En/')->
124
          matches('/Fr/')->
125
          matches('/Title/')->
126
          matches('/Body/')->
127
          matches('/Slug/')->
128
          matches('/Jonathan H. Wage/')->
129
          matches('/Fabien POTENCIER/')->
130
        end()->
131
        with('request')->begin()->
132
          isParameter('module', 'articles')->
133
          isParameter('action', 'new')->
134
        end()->
135
      click('Save', array('article' => $info))->
136
        with('response')->begin()->
137
          isRedirected()->
138
          followRedirect()->
139
        end()->
140
        with('doctrine')->begin()->
141
          check('Article', array('is_on_homepage' => $info['is_on_homepage']))->
142
          check('ArticleTranslation', array('lang' => 'fr', 'title' => 'Test French title'))->
143
          check('ArticleTranslation', array('lang' => 'en', 'title' => 'Test English title'))->
144
        end()
145
    ;
146
  }
147
 
148
  protected function _testEnumDropdown()
149
  {
150
    $this->info('Test enum column type uses a dropdown as the widget');
151
 
152
    $this->
153
      get('/subscriptions/new')->
154
        with('response')->begin()->
155
          checkElement('select', 'NewActivePendingExpired')->
156
        end()
157
    ;
158
  }
159
 
160
  protected function _testUserEmbedsProfileForm()
161
  {
162
    $this->info('Test user form embeds the profile form');
163
 
164
    $this->
165
      get('/users/new')->
166
        with('response')->begin()->
167
          matches('/Profile/')->
168
          matches('/First name/')->
169
          matches('/Last name/')->
170
        end()
171
    ;
172
 
173
    $this->info('Test the Profile form saves and attached to user properly');
174
 
175
    $userInfo = array(
176
      'user' => array(
177
        'username'         => 'test',
178
        'password'         => 'test',
179
        'groups_list'      => array(1, 2),
180
        'permissions_list' => array(3, 4),
181
        'Profile'  => array(
182
          'first_name' => 'Test',
183
          'last_name'  => 'Test'
184
        )
185
      )
186
    );
187
 
188
    $this->click('Save', $userInfo);
189
 
190
    $user = Doctrine_Core::getTable('User')->findOneByUsername($userInfo['user']['username']);
191
    $userInfo['user']['Profile']['user_id'] = $user->id;
192
 
193
    $this->
194
        with('response')->begin()->
195
          isRedirected()->
196
          followRedirect()->
197
        end()->
198
        with('doctrine')->begin()->
199
          check('User', array('username' => $userInfo['user']['username']))->
200
          check('Profile', $userInfo['user']['Profile'])->
201
          check('UserGroup', array('user_id' => $user->id, 'group_id' => $user->Groups[0]->id))->
202
          check('UserGroup', array('user_id' => $user->id, 'group_id' => $user->Groups[1]->id))->
203
          check('UserPermission', array('user_id' => $user->id, 'permission_id' => $user->Permissions[0]->id))->
204
          check('UserPermission', array('user_id' => $user->id, 'permission_id' => $user->Permissions[1]->id))->
205
        end()
206
    ;
207
 
208
    unset($userInfo['user']['Profile']['user_id']);
209
    $tester = $this->get('/users/new')->
210
      click('Save', $userInfo)->
211
      with('form')->begin();
212
    $tester->hasErrors();
213
    $form = $tester->getForm();
214
    $this->test()->is((string) $form->getErrorSchema(), 'username [An object with the same "username" already exist.]', 'Check username gives unique error');
215
    $tester->end();
216
  }
217
 
218
  protected function _runAdminGenModuleSanityCheck($model, $module)
219
  {
220
    $this->info('Running admin gen sanity check for module "' . $module . '"');
221
    $record = Doctrine_Core::getTable($model)
222
      ->createQuery('a')
223
      ->fetchOne();
224
 
225
    $this->
226
      info('Sanity check on "' . $module . '" module')->
227
      getAndCheck($module, 'index', '/' . $module)->
228
      get('/' . $module . '/' . $record->getId() . '/edit');
229
 
230
    $this
231
      ->click('Save')->
232
        with('response')->begin()->
233
          isRedirected()->
234
          followRedirect()->
235
        end()
236
    ;
237
  }
238
 
239
  protected function _generateAdminGenModule($model, $module)
240
  {
241
    $this->info('Generating admin gen module "' . $module . '"');
242
    $task = new sfDoctrineGenerateAdminTask($this->getContext()->getEventDispatcher(), new sfFormatter());
243
    $task->run(array('application' => 'backend', 'route_or_model' => $model));
244
  }
245
 
246
  protected function _generateAdminGenModules()
247
  {
248
    // $task = new sfDoctrineBuildAllReloadTask($this->getContext()->getEventDispatcher(), new sfFormatter());
249
    // $task->run(array(), array('--no-confirmation'));
250
 
251
    // Generate the admin generator modules
252
    foreach ($this->_modules as $model => $module)
253
    {
254
      $this->_generateAdminGenModule($model, $module);
255
    }
256
  }
257
 
258
  protected function _cleanupAdminGenModules()
259
  {
260
    $fs = new sfFilesystem($this->getContext()->getEventDispatcher(), new sfFormatter());
261
    foreach ($this->_modules as $module)
262
    {
263
      $this->info('Removing admin gen module "' . $module . '"');
264
      $fs->execute('rm -rf ' . sfConfig::get('sf_app_module_dir') . '/' . $module);
265
    }
266
    $fs->execute('rm -rf ' . sfConfig::get('sf_test_dir') . '/functional/backend');
267
    $fs->execute('rm -rf ' . sfConfig::get('sf_data_dir') . '/*.sqlite');
268
  }
269
 
270
  protected function _getQueryExecutionEvents()
271
  {
272
    $events = array();
273
 
274
    $databaseManager = $this->browser->getContext()->getDatabaseManager();
275
    foreach ($databaseManager->getNames() as $name)
276
    {
277
      $database = $databaseManager->getDatabase($name);
278
      if ($database instanceof sfDoctrineDatabase && $profiler = $database->getProfiler())
279
      {
280
        foreach ($profiler->getQueryExecutionEvents() as $event)
281
        {
282
          $events[$event->getSequence()] = $event;
283
        }
284
      }
285
    }
286
 
287
    ksort($events);
288
 
289
    return array_values($events);
290
  }
291
 
292
  public function __destruct()
293
  {
294
    $this->_cleanupAdminGenModules();
295
  }
296
}