-
Notifications
You must be signed in to change notification settings - Fork 676
/
TestOfUpgradeController.php
896 lines (795 loc) · 40.1 KB
/
TestOfUpgradeController.php
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
<?php
/**
*
* ThinkUp/tests/TestOfUpgradeController.php
*
* Copyright (c) 2009-2011 Mark Wilkie
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkupapp.com).
*
* ThinkUp 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.
*
* ThinkUp 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 ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
*
* TestOfUpgradeController
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2011 Mark Wilkie
* @author Mark Wilkie <mwilkie[at]gmail[dot]com>
*
*/
require_once dirname(__FILE__).'/init.tests.php';
require_once THINKUP_ROOT_PATH.'webapp/_lib/extlib/simpletest/autorun.php';
require_once THINKUP_ROOT_PATH.'webapp/config.inc.php';
require_once THINKUP_ROOT_PATH.'webapp/_lib/extlib/simpletest/web_tester.php';
class TestOfUpgradeController extends ThinkUpUnitTestCase {
const TEST_TABLE = 'options';
public function setUp(){
parent::setUp();
$config = Config::getInstance();
$dao = DAOFactory::getDAO('OptionDAO');
$this->pdo = OptionMysqlDAO::$PDO;
$this->init_db_version = $config->getValue('THINKUP_VERSION');
$config->setValue('THINKUP_VERSION', $config->getValue('THINKUP_VERSION') + 10); //set a high version num
$this->token_file = THINKUP_WEBAPP_PATH . UpgradeController::CACHE_DIR . '/.htupgrade_token';
$this->migrations_test_dir = THINKUP_ROOT_PATH . 'tests/data/migrations/';
$this->migrations_dir = THINKUP_WEBAPP_PATH . 'install/sql/mysql_migrations/';
$this->migrations_file1 = 'migration1.sql';
$this->migrations_file2 = 'migration2.sql';
$this->migrations_file3 = 'migration3.sql';
}
public function tearDown(){
parent::tearDown();
$config = Config::getInstance();
//reset app version
$config->setValue('THINKUP_VERSION', $this->init_db_version);
/** delete files if needed **/
// delete token
if (file_exists($this->token_file)) {
unlink($this->token_file);
}
// delete migration test files
if (isset($this->test_migrations) && count($this->test_migrations) > 0) {
foreach($this->test_migrations as $file) {
if (file_exists($file)) {
unlink($file);
}
}
$this->test_migrations = array();
}
}
public function testConstructor() {
$controller = new UpgradeController(true);
$this->assertTrue(isset($controller));
}
public function testNoMigrationNeeded() {
$this->simulateLogin('me@example.com', true);
$controller = new UpgradeController(true);
$results = $controller->go();
$this->assertPattern('/<!-- no upgrade needed -->/', $results);
}
public function testDatabaseMigrationNeeded() {
// create test migration sql
$this->migrationFiles(1);
$this->simulateLogin('me@example.com', true);
$controller = new UpgradeController(true);
$results = $controller->go();
$this->assertPattern('/needs 1 database update/', $results);
$v_mgr = $controller->getViewManager();
$queries = $v_mgr->getTemplateDataItem('migrations');
$this->assertEqual(1, count($queries), 'one migration query');
$this->assertEqual(file_get_contents($this->test_migrations[0]), $queries[0]['sql']);
$this->test_migrations = array(); //clear out old data
$this->migrationFiles(2);
$results = $controller->go();
$this->assertPattern('/needs 2 database updates/', $results);
$v_mgr = $controller->getViewManager();
$queries = $v_mgr->getTemplateDataItem('migrations');
$this->assertEqual(2, count($queries), 'two migration query');
$this->assertEqual(file_get_contents($this->test_migrations[0]), $queries[1]['sql']);
$this->assertEqual(file_get_contents($this->test_migrations[1]), $queries[0]['sql']);
}
public function testDatabaseMigrationNeededHighTableCount() {
// create test migration sql
$this->migrationFiles(1);
// table row counts are good
$this->simulateLogin('me@example.com', true);
$controller = new UpgradeController(true);
$results = $controller->go();
$this->assertPattern('/needs 1 database update/', $results);
$v_mgr = $controller->getViewManager();
$this->assertNull($v_mgr->getTemplateDataItem('high_table_row_count') ) ;
// table row counts are bad
$old_count = UpgradeController::$WARN_TABLE_ROW_COUNT;
UpgradeController::$WARN_TABLE_ROW_COUNT = 2;
$this->simulateLogin('me@example.com', true);
$controller = new UpgradeController(true);
$results = $controller->go();
$this->assertPattern('/we recommend that you use the.*command line upgrade tool.*when upgrading ThinkUp/sm',
$results);
$this->assertPattern('/needs 1 database update/', $results);
$v_mgr = $controller->getViewManager();
$table_counts = $v_mgr->getTemplateDataItem('high_table_row_count');
$this->assertNotNull($table_counts);
$this->assertNotNull(3, $table_counts['count']); // tu_plugins, defaults to three
UpgradeController::$WARN_TABLE_ROW_COUNT = $old_count;
}
public function testGetMigrationList() {
$this->simulateLogin('me@example.com', true);
$controller = new UpgradeController(true);
$this->migrationFiles(1);
$this->newMigrationFiles('some_stuff');
$this->newMigrationFiles('some_stuff', $old = true); // older already ran?
$db_version = UpgradeController::getCurrentDBVersion($cached = false);
$list = $controller->getMigrationList($db_version);
$this->assertEqual(count($list), 2);
$this->assertTrue($list[0]['new_migration']);
$this->assertTrue($list[1]['new_migration']);
$this->assertPattern("/^2010-09-17_v\d+\.\d+\.sql\.migration$/",$list[0]['filename']);
$this->assertPattern("/^2011-09-21_some_stuff_v\d+\.\d+\.sql$/",$list[1]['filename']);
//var_dump($list);
}
public function testGetMigrationListWithNewSQL() {
$this->simulateLogin('me@example.com', true);
$controller = new UpgradeController(true);
$this->migrationFiles(1);
$this->newMigrationFiles('some_stuff');
$this->newMigrationFiles('some_stuff2', $old = false, $add_sql = false, $no_version = true);
$db_version = UpgradeController::getCurrentDBVersion($cached = false);
$list = $controller->getMigrationList($db_version, $no_version = true);
$clean_list = array();
$cnt = 0;
foreach($list as $migration) {
if (preg_match("/some_stuff/", $migration['filename'])) {
array_push($clean_list, $migration);
}
$cnt++;
}
$this->assertTrue($clean_list[0]['new_migration']);
$this->assertTrue($clean_list[1]['new_migration']);
$this->assertPattern("/^2011-09-21_some_stuff2.sql$/",$clean_list[0]['filename']);
$this->assertPattern("/^2011-09-21_some_stuff_v1".$db_version.".sql$/",$clean_list[1]['filename']);
// run migration
$install_dao = DAOFactory::getDAO('InstallerDAO');
foreach($clean_list as $migration) {
$sql = preg_replace('/\-\-.*/','', $migration['sql']);
$install_dao->runMigrationSQL($sql, $migration['new_migration'], $migration['filename']);
}
$stmt = $this->pdo->query("select * from " . $this->table_prefix . "completed_migrations");
$data = $stmt->fetchAll();
$this->assertEqual(count($data), 5);
$this->assertEqual($data[0]['migration'], '2011-09-21_some_stuff2-0');
$this->assertEqual($data[1]['migration'], '2011-09-21_some_stuff2-1');
$this->assertEqual($data[2]['migration'], '2011-09-21_some_stuff-0');
$this->assertEqual($data[3]['migration'], '2011-09-21_some_stuff-1');
$this->assertEqual($data[4]['migration'], '2011-09-21_some_stuff-2');
// run same migration file now as a versioned file with one new sql line
$migration = $clean_list[0];
$filename = $migration['filename'] = '2011-09-21_some_stuff2_v1.1.sql';
$migration['sql'] .= "\nINSERT INTO " . $this->table_prefix . "test1 (value) VALUES (5);";
$sql = preg_replace('/\-\-.*/','', $migration['sql']);
$install_dao->runMigrationSQL($sql, $migration['new_migration'], $filename);
$stmt = $this->pdo->query("select * from " . $this->table_prefix . "completed_migrations");
$data2 = $stmt->fetchAll();
$this->assertEqual(count($data2), 6);
$this->assertEqual($data2[0]['migration'], '2011-09-21_some_stuff2-0');
$this->assertEqual($data2[1]['migration'], '2011-09-21_some_stuff2-1');
$this->assertEqual($data2[2]['migration'], '2011-09-21_some_stuff-0');
$this->assertEqual($data2[3]['migration'], '2011-09-21_some_stuff-1');
$this->assertEqual($data2[4]['migration'], '2011-09-21_some_stuff-2');
$this->assertEqual($data2[5]['migration'], '2011-09-21_some_stuff2-2');
}
public function testRunNewMigration() {
$this->simulateLogin('me@example.com', true);
$controller = new UpgradeController(true);
$this->newMigrationFiles('some_stuff');
$this->newMigrationFiles('some_stuff', $old = true); // older already ran?
$db_version = UpgradeController::getCurrentDBVersion($cached = false);
$list = $controller->getMigrationList($db_version);
$this->assertEqual(count($list), 1);
$this->assertTrue($list[0]['new_migration']);
$this->assertPattern("/^2011-09-21_some_stuff_v\d+\.\d+\.sql$/",$list[0]['filename']);
$_GET['migration_index'] = 1;
$results = $controller->go();
$obj = json_decode($results);
$this->assertTrue($obj->processed);
$sql = file_get_contents($this->test_migrations[0]);
$sql = preg_replace('/\-\-.*/','', $sql);
$this->assertEqual($obj->sql, $sql);
$sql = "show tables like '" . $this->table_prefix . "test1'";
$stmt = $this->pdo->query($sql);
$data = $stmt->fetch();
$this->assertEqual($data[0], $this->table_prefix . "test1");
$sql = 'select * from ' . $this->table_prefix . "test1";
$stmt = $this->pdo->query($sql);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assertEqual(count($data), 3);
$this->assertEqual($data[0]['value'], 1);
$this->assertEqual($data[1]['value'], 2);
$this->assertEqual($data[2]['value'], 3);
// tu_completed_migrations table should contain a record for our latest migration
$stmt = $this->pdo->query("select * from " . $this->table_prefix . "completed_migrations");
$data = $stmt->fetchAll();
$this->assertEqual(count($data), 3);
$this->assertEqual($data[0]['migration'], '2011-09-21_some_stuff-0');
$this->assertEqual($data[1]['migration'], '2011-09-21_some_stuff-1');
$this->assertEqual($data[2]['migration'], '2011-09-21_some_stuff-2');
// run it againto veriy it skips alrready run migrations, but add a new one as well
$new_sql = "INSERT INTO " . $this->table_prefix . "test1 (value) VALUES (4),(5),(6);";
$this->newMigrationFiles('some_stuff', false, $new_sql);
$_GET['migration_index'] = 1;
$results = $controller->go();
$obj = json_decode($results);
$this->assertTrue($obj->processed);
$sql = file_get_contents($this->test_migrations[0]);
$sql = preg_replace('/\-\-.*/','', $sql);
$this->assertEqual($obj->sql, $sql);
$sql = 'select * from ' . $this->table_prefix . 'test1';
$stmt = $this->pdo->query($sql);
$data2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assertEqual(count($data2), 6);
$this->assertEqual($data2[0]['value'], 1);
$this->assertEqual($data2[1]['value'], 2);
$this->assertEqual($data2[2]['value'], 3);
$this->assertEqual($data2[3]['value'], 4);
$this->assertEqual($data2[4]['value'], 5);
$this->assertEqual($data2[5]['value'], 6);
$stmt = $this->pdo->query("select * from " . $this->table_prefix . "completed_migrations");
$data2 = $stmt->fetchAll();
$this->assertEqual(count($data2), 4);
$this->assertEqual($data2[0]['migration'], '2011-09-21_some_stuff-0');
$this->assertEqual($data2[1]['migration'], '2011-09-21_some_stuff-1');
$this->assertEqual($data2[2]['migration'], '2011-09-21_some_stuff-2');
$this->assertEqual($data2[3]['migration'], '2011-09-21_some_stuff-3');
$this->assertEqual($data2[0]['date_ran'], $data[0]['date_ran']);
$this->assertEqual($data2[1]['date_ran'], $data[1]['date_ran']);
$this->assertEqual($data2[2]['date_ran'], $data[2]['date_ran']);
}
public function testRunNewFailedMigration() {
$this->simulateLogin('me@example.com', true);
$controller = new UpgradeController(true);
$this->newMigrationFiles2('some_stuff2', $date = false, $bad_sql = true);
$db_version = UpgradeController::getCurrentDBVersion($cached = false);
$list = $controller->getMigrationList($db_version);
$this->assertEqual(count($list), 1);
$this->assertTrue($list[0]['new_migration']);
$this->assertPattern("/^2011-09-21_some_stuff2_v\d+\.\d+\.sql$/",$list[0]['filename']);
$_GET['migration_index'] = 1;
$results = $controller->go();
$obj = json_decode($results);
$this->assertFalse($obj->processed);
$stmt = $this->pdo->query("select * from " . $this->table_prefix . "completed_migrations");
$data2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assertEqual(5, count($data2));
$stmt = $this->pdo->query("desc " . $this->table_prefix . "test3");
$stmt->fetch(PDO::FETCH_ASSOC);
$stmt->fetch(PDO::FETCH_ASSOC);
$this->assertFalse($stmt->fetch(PDO::FETCH_ASSOC)); // no user_id column
$stmt = $this->pdo->query("select count(*) as count from " . $this->table_prefix . "test3");
$data = $stmt->fetch();
$this->assertEqual(3, $data['count']);
$controller = new UpgradeController(true);
$this->newMigrationFiles2('some_stuff2', $date = false, $bad_sql = false);
$results = $controller->go();
$obj = json_decode($results);
$this->assertTrue($obj->processed);
$stmt = $this->pdo->query("select * from " . $this->table_prefix . "completed_migrations");
$data2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assertEqual(9, count($data2));
$stmt = $this->pdo->query("desc " . $this->table_prefix . "test3");
$stmt->fetch(PDO::FETCH_ASSOC);
$stmt->fetch(PDO::FETCH_ASSOC);
$this->assertTrue($stmt->fetch(PDO::FETCH_ASSOC)); // has a user_id column
$stmt = $this->pdo->query("select * from " . $this->table_prefix . "test3 where value = 8");
$data2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assertEqual($data2[0]['value'], '8');
$this->assertEqual($data2[0]['user_id'], '1003');
$stmt = $this->pdo->query("select * from " . $this->table_prefix . "test3 where value = 5");
$data2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assertEqual($data2[0]['value'], '5');
$this->assertEqual($data2[0]['user_id'], '1000');
}
public function testRunNewMigrationUpdateCompletedTable() {
$this->simulateLogin('me@example.com', true);
$controller = new UpgradeController(true);
$this->newMigrationFiles('some_stuff');
$this->newMigrationFiles('some_stuff', $old = true); // older already ran?
$db_version = UpgradeController::getCurrentDBVersion($cached = false);
$list = $controller->getMigrationList($db_version);
$this->assertEqual(count($list), 1);
$this->assertTrue($list[0]['new_migration']);
$this->assertPattern("/^2011-09-21_some_stuff_v\d+\.\d+\.sql$/",$list[0]['filename']);
// create completion table
$com_sql_file = THINKUP_ROOT_PATH.'webapp/install/sql/completed_migrations.sql';
//echo $com_sql_file;
$com_sql = file_get_contents($com_sql_file);
$this->pdo->query($com_sql);
$this->pdo->query("alter table " . $this->table_prefix . "completed_migrations DROP column sql_ran");
$_GET['migration_index'] = 1;
$results = $controller->go();
$obj = json_decode($results);
$this->assertTrue($obj->processed);
$sql = file_get_contents($this->test_migrations[0]);
$sql = preg_replace('/\-\-.*/','', $sql);
$this->assertEqual($obj->sql, $sql);
$stmt = $this->pdo->query("select * from " . $this->table_prefix . "completed_migrations");
$data2 = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assertEqual(count($data2), 3);
$this->assertPattern('/CREATE TABLE `.*test1`/', $data2[0]['sql_ran']);
}
public function testGenerateUpgradeToken() {
$this->simulateLogin('me@example.com');
$controller = new UpgradeController(true);
UpgradeController::generateUpgradeToken();
$this->assertTrue( file_exists($this->token_file) );
$this->assertPattern('/^[\da-f]{32}$/', file_get_contents($this->token_file));
}
/**
* Test generating and authenticating with a token
*/
public function testNotLoggedInNoTAdminGensAndAuthsToken() {
// not logged in...
$controller = new UpgradeController(true);
$results = $controller->go();
$this->assertTrue( file_exists($this->token_file) );
$this->assertPattern('/<!-- we are upgrading -->/', $results);
$this->assertTrue( file_exists($this->token_file) );
$token = file_get_contents($this->token_file);
$this->assertPattern('/^[\da-f]{32}$/', $token);
// logged in, but not an admin
unlink($this->token_file);
$this->simulateLogin('me@example.com', false);
$results = $controller->go();
$this->assertTrue( file_exists($this->token_file) );
$this->assertPattern('/<!-- we are upgrading -->/', $results);
$this->assertTrue( file_exists($this->token_file) );
$token = file_get_contents($this->token_file);
$this->assertPattern('/^[\da-f]{32}$/', $token);
// login with a bad token
$_GET['upgrade_token'] = 'badtoken';
$results = $controller->go();
$this->assertPattern('/This update has already been completed/', $results);
// log in with a valid token
$this->simulateLogin('me@example.com', false);
$_GET['upgrade_token'] = $token;
$results = $controller->go();
$this->assertFalse( file_exists($this->token_file) );
$this->assertPattern('/<!-- no upgrade needed -->/', $results);
// NOTE: this will only happen when our db versions are out of sync
$this->assertPattern('/database version has been updated to reflect the latest installed version/', $results);
}
/**
* Test generating and emailing a token to admin(s)
*/
public function testTokenEmail() {
$config = Config::getInstance();
$site_root_path = $config->getValue('site_root_path');
// build 1 valid admin and two invalid admins
$builder1 = FixtureBuilder::build('owners', array('is_admin' => 1, 'is_activated' => 1, 'email' => 'm@w.nz'));
$builder2 = FixtureBuilder::build('owners', array('is_admin' => 0, 'is_activated' => 1, 'email' => 'm2@w.nz'));
$builder3 = FixtureBuilder::build('owners', array('is_admin' => 1, 'is_activated' => 0, 'email' => 'm3@w.nz'));
$_SERVER['HTTP_HOST'] = "mytestthinkup";
$controller = new UpgradeController(true);
$results = $controller->go();
$this->assertTrue( file_exists($this->token_file) );
$this->assertPattern('/<!-- we are upgrading -->/', $results);
$this->assertTrue( file_exists($this->token_file) );
$token = file_get_contents($this->token_file);
$this->assertPattern('/^[\da-f]{32}$/', $token);
$email_file = Mailer::getLastMail();
$this->debug($email_file);
$this->assertPattern('/to\: m@w\.nz\s/', $email_file);
$this->assertPattern('/subject\: Upgrade Your ThinkUp Database/', $email_file);
$token_regex = '/http:\/\/mytestthinkup'.str_replace('/', '\/', $site_root_path).
'install\/upgrade.php\?upgrade_token=' . $token . '/';
$this->assertPattern($token_regex, $email_file);
// build 1 more valid admin, should have two to emails
$test_email = THINKUP_WEBAPP_PATH . UpgradeController::CACHE_DIR . Mailer::EMAIL;
unlink($test_email);
unlink($this->token_file);
$builder4 = FixtureBuilder::build('owners', array('is_admin' => 1, 'is_activated' => 1, 'email' => 'm4@w.nz'));
$results = $controller->go();
$this->assertTrue( file_exists($this->token_file) );
$this->assertPattern('/<!-- we are upgrading -->/', $results);
$this->assertTrue( file_exists($this->token_file) );
$token = file_get_contents($this->token_file);
$this->assertPattern('/^[\da-f]{32}$/', $token);
$email_file = Mailer::getLastMail();
$this->assertPattern('/to\: m@w\.nz,m4@w\.nz\s/', $email_file);
$this->assertPattern('/subject\: Upgrade Your ThinkUp Database/', $email_file);
$token_regex = '/\/install\/upgrade.php\?upgrade_token=' . $token . '/';
$this->assertPattern($token_regex, $email_file);
// should not send email if a token file exists
$test_email = THINKUP_WEBAPP_PATH . UpgradeController::CACHE_DIR . Mailer::EMAIL;
unlink($test_email);
$results = $controller->go();
$this->assertFalse( file_exists($test_email) );
}
public function testTokenEmailWithSSL() {
$config = Config::getInstance();
$site_root_path = $config->getValue('site_root_path');
// build 1 valid admin and two invalid admins
$builder1 = FixtureBuilder::build('owners', array('is_admin' => 1, 'is_activated' => 1, 'email' => 'm@w.nz'));
$builder2 = FixtureBuilder::build('owners', array('is_admin' => 0, 'is_activated' => 1, 'email' => 'm2@w.nz'));
$builder3 = FixtureBuilder::build('owners', array('is_admin' => 1, 'is_activated' => 0, 'email' => 'm3@w.nz'));
$_SERVER['HTTP_HOST'] = "mytestthinkup";
$_SERVER['HTTPS'] = "mytestthinkup";
$controller = new UpgradeController(true);
$results = $controller->go();
$this->assertTrue( file_exists($this->token_file) );
$this->assertPattern('/<!-- we are upgrading -->/', $results);
$this->assertTrue( file_exists($this->token_file) );
$token = file_get_contents($this->token_file);
$this->assertPattern('/^[\da-f]{32}$/', $token);
$email_file = Mailer::getLastMail();
$this->debug($email_file);
$this->assertPattern('/to\: m@w\.nz\s/', $email_file);
$this->assertPattern('/subject\: Upgrade Your ThinkUp Database/', $email_file);
$token_regex = '/https:\/\/mytestthinkup'.str_replace('/', '\/', $site_root_path).
'install\/upgrade.php\?upgrade_token=' . $token . '/';
$this->assertPattern($token_regex, $email_file);
// build 1 more valid admin, should have two to emails
$test_email = THINKUP_WEBAPP_PATH . UpgradeController::CACHE_DIR . Mailer::EMAIL;
unlink($test_email);
unlink($this->token_file);
$builder4 = FixtureBuilder::build('owners', array('is_admin' => 1, 'is_activated' => 1, 'email' => 'm4@w.nz'));
$results = $controller->go();
$this->assertTrue( file_exists($this->token_file) );
$this->assertPattern('/<!-- we are upgrading -->/', $results);
$this->assertTrue( file_exists($this->token_file) );
$token = file_get_contents($this->token_file);
$this->assertPattern('/^[\da-f]{32}$/', $token);
$email_file = Mailer::getLastMail();
$this->assertPattern('/to\: m@w\.nz,m4@w\.nz\s/', $email_file);
$this->assertPattern('/subject\: Upgrade Your ThinkUp Database/', $email_file);
$token_regex = '/\/install\/upgrade.php\?upgrade_token=' . $token . '/';
$this->assertPattern($token_regex, $email_file);
// should not send email if a token file exists
$test_email = THINKUP_WEBAPP_PATH . UpgradeController::CACHE_DIR . Mailer::EMAIL;
unlink($test_email);
$results = $controller->go();
$this->assertFalse( file_exists($test_email) );
}
public function testProcessOneMigrations() {
$this->simulateLogin('me@example.com', true);
$controller = new UpgradeController(true);
$this->migrationFiles(1);
$_GET['migration_index'] = 1;
$results = $controller->go();
$obj = json_decode($results);
$this->assertTrue($obj->processed);
$sql_file = file_get_contents($this->test_migrations[0]);
$sql_file = preg_replace('/\-\-.*/','', $sql_file);
$this->assertEqual($obj->sql, $sql_file);
$sql = "show tables like '" . $this->table_prefix . "test1'";
$stmt = $this->pdo->query($sql);
$data = $stmt->fetch();
$this->assertEqual($data[0], $this->table_prefix . 'test1');
$sql = 'select * from ' . $this->table_prefix . 'test1';
$stmt = $this->pdo->query($sql);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assertEqual(count($data), 3);
}
public function testProcessMinorVersionMigration() {
$this->simulateLogin('me@example.com', true);
$config = Config::getInstance();
$version = $config->getValue('THINKUP_VERSION') + 10;
$version .= '.2';
$config->setValue('THINKUP_VERSION', $version); //set a high minor version
$controller = new UpgradeController(true);
$this->migrationFiles(1);
$_GET['migration_index'] = 1;
$results = $controller->go();
$obj = json_decode($results);
$this->assertTrue($obj->processed);
$sql = file_get_contents($this->test_migrations[0]);
$sql = preg_replace('/\-\-.*/','', $sql);
$this->assertEqual($obj->sql, $sql);
$sql = "show tables like '" . $this->table_prefix . "test1'";
$stmt = $this->pdo->query($sql);
$data = $stmt->fetch();
$this->assertEqual($data[0], $this->table_prefix . 'test1');
$sql = 'select * from ' . $this->table_prefix . 'test1';
$stmt = $this->pdo->query($sql);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assertEqual(count($data), 3);
}
public function testProcessMinorBetaVersionMigration() {
$this->simulateLogin('me@example.com', true);
$config = Config::getInstance();
$test_version = $config->getValue('THINKUP_VERSION') + 10.1;
$test_version .= '.2beta';
$config->setValue('THINKUP_VERSION', $test_version); //set a high minor version with beta string
$controller = new UpgradeController(true);
$this->migrationFiles(1);
$_GET['migration_index'] = 1;
$results = $controller->go();
$obj = json_decode($results);
$this->assertTrue($obj->processed);
$sql = file_get_contents($this->test_migrations[0]);
$sql = preg_replace('/\-\-.*/','', $sql);
$this->assertEqual($obj->sql, $sql);
$sql = "show tables like '" . $this->table_prefix . "test1'";
$stmt = $this->pdo->query($sql);
$data = $stmt->fetch();
$this->assertEqual($data[0], $this->table_prefix . 'test1');
$sql = 'select * from ' . $this->table_prefix . 'test1';
$stmt = $this->pdo->query($sql);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assertEqual(count($data), 3);
}
public function testProcessSnowflakeMigration() {
$config = Config::getInstance();
$app_path = $config->getValue('source_root_path');
$snowflakekey = 'runnig_snowflake_uprade';
// no snowflake update needed...
$this->pdo->query("truncate table " . $this->table_prefix . "options");
$this->simulateLogin('me@example.com', true);
$this->assertFalse(SessionCache::isKeySet($snowflakekey));
$config = Config::getInstance();
$config->setValue('THINKUP_VERSION', '0.4');
$controller = new UpgradeController(true);
$results = $controller->go();
//print $results;
$this->assertPattern('/needs 1 database update/', $results);
// snowflake update needed...
$this->pdo->query("drop table " . $this->table_prefix . "options");
$this->testdb_helper->runSQL('ALTER TABLE ' . $this->table_prefix .
'instances CHANGE last_post_id last_status_id bigint(11) NOT NULL');
$this->testdb_helper->runSQL('ALTER TABLE ' . $this->table_prefix .'links ADD post_id BIGINT( 20 ) NOT NULL,'.
'ADD network VARCHAR( 20 ) NOT NULL');
$controller = new UpgradeController(true);
$results = $controller->go();
$this->assertPattern('/needs 2 database updates/', $results);
$v_mgr = $controller->getViewManager();
$queries = $v_mgr->getTemplateDataItem('migrations');
$this->assertEqual(2, count($queries), 'two migration queries');
$this->assertTrue(SessionCache::isKeySet($snowflakekey));
// run snowflake migration
$_GET['migration_index'] = 1;
$controller = new UpgradeController(true);
$results = $controller->go();
$obj = json_decode($results);
$this->assertTrue($obj->processed);
$stmt = $this->pdo->query("desc " . $this->table_prefix . "instances last_post_id");
$data = $stmt->fetch();
$this->assertEqual($data['Field'], 'last_post_id');
$this->assertPattern('/bigint\(20\)\s+unsigned/i', $data['Type']);
$this->assertTrue(SessionCache::isKeySet($snowflakekey));
// run version 4 upgrade
$_GET['migration_index'] = 2;
$controller = new UpgradeController(true);
$results = $controller->go();
$this->assertTrue($obj->processed);
$stmt = $this->pdo->query("desc " . $this->table_prefix . "instances last_post_id");
$data = $stmt->fetch();
$this->assertEqual($data['Field'], 'last_post_id');
$this->assertPattern('/bigint\(20\)\s+unsigned/i', $data['Type']);
// no snowflake session data when complete
$config = Config::getInstance();
unset($_GET['migration_index']);
$_GET['migration_done'] = true;
$results = $controller->go();
$obj = json_decode($results);
$this->assertTrue($obj->migration_complete);
$this->assertFalse(SessionCache::isKeySet($snowflakekey));
}
public function testProcessTwoMigrations() {
$this->simulateLogin('me@example.com', true);
$controller = new UpgradeController(true);
$this->migrationFiles(2);
$_GET['migration_index'] = 1;
$results = $controller->go();
$obj = json_decode($results);
$this->assertTrue($obj->processed);
$sql = file_get_contents($this->test_migrations[1]);
$sql = preg_replace('/\-\-.*/','', $sql);
$this->assertEqual($obj->sql, $sql);
$sql = "show tables like '" . $this->table_prefix . "test2'";
$stmt = $this->pdo->query($sql);
$data = $stmt->fetch();
$this->assertEqual($data[0], $this->table_prefix . 'test2');
$sql = 'select * from ' . $this->table_prefix . 'test2';
$stmt = $this->pdo->query($sql);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assertEqual(count($data), 1);
$_GET['migration_index'] = 2;
$results = $controller->go();
$obj = json_decode($results);
$this->assertTrue($obj->processed);
$sql = file_get_contents($this->test_migrations[0]);
$sql = preg_replace('/\-\-.*/','', $sql);
$this->assertEqual($obj->sql, $sql);
$sql = "show tables like '" . $this->table_prefix . "test1'";
$stmt = $this->pdo->query($sql);
$data = $stmt->fetch();
$this->assertEqual($data[0], $this->table_prefix . 'test1');
$sql = 'select * from ' . $this->table_prefix . 'test1';
$stmt = $this->pdo->query($sql);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assertEqual(count($data), 3);
}
public function testProcessMigrationWithAToken() {
// not logged in...
$controller = new UpgradeController(true);
$results = $controller->go();
$this->assertTrue( file_exists($this->token_file) );
$this->assertPattern('/<!-- we are upgrading -->/', $results);
$this->assertTrue( file_exists($this->token_file) );
$token = file_get_contents($this->token_file);
$this->assertPattern('/^[\da-f]{32}$/', $token);
$controller = new UpgradeController(true);
$this->migrationFiles(1);
$_GET['migration_index'] = 1;
$_GET['upgrade_token'] = $token;
$results = $controller->go();
$obj = json_decode($results);
$this->assertTrue($obj->processed);
$sql = file_get_contents($this->test_migrations[0]);
$sql = preg_replace('/\-\-.*/','', $sql);
$this->assertEqual($obj->sql, $sql);
}
public function testMigrationDone() {
// no db record...
$config = Config::getInstance();
$app_version = $config->getValue('THINKUP_VERSION');
$this->simulateLogin('me@example.com', true);
$controller = new UpgradeController(true);
$_GET['migration_done'] = true;
$results = $controller->go();
$sql = "select * from " . $this->table_prefix . 'options';
$stmt = $this->pdo->query($sql);
$data = $stmt->fetch();
$this->assertEqual($data['option_value'], $app_version);
// with a db record...
$config->setValue('THINKUP_VERSION', ($config->getValue('THINKUP_VERSION') + 1 ));
$results = $controller->go();
$sql = "select * from " . $this->table_prefix . 'options';
$stmt = $this->pdo->query($sql);
$data = $stmt->fetch();
$this->assertEqual($data['option_value'], ($app_version + 1));
}
public function testMigrationDoneWithToken() {
// not logged in...
$controller = new UpgradeController(true);
$results = $controller->go();
$this->assertTrue( file_exists($this->token_file) );
$this->assertPattern('/<!-- we are upgrading -->/', $results);
$this->assertTrue( file_exists($this->token_file) );
$token = file_get_contents($this->token_file);
$this->assertPattern('/^[\da-f]{32}$/', $token);
$config = Config::getInstance();
$app_version = $config->getValue('THINKUP_VERSION');
$this->simulateLogin('me@example.com', true);
$controller = new UpgradeController(true);
$_GET['migration_done'] = true;
$_GET['upgrade_token'] = $token;
$results = $controller->go();
$sql = "select * from " . $this->table_prefix . 'options';
$stmt = $this->pdo->query($sql);
$data = $stmt->fetch();
$this->assertEqual($data['option_value'], $app_version);
$this->assertFalse( file_exists($this->token_file) );
}
public function testProcessMigrationsDifferentPrefix() {
$config = Config::getInstance();
$config->setValue('table_prefix', 'new_prefix_');
$stmt = $this->pdo->query("show tables");
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
//var_dump($data);
foreach($data as $table) {
foreach($table as $key=> $value) {
$new_value = preg_replace("/tu_/", " new_prefix_", $value);
$sql = "RENAME TABLE $value TO $new_value";
$this->pdo->query($sql);
}
}
$this->simulateLogin('me@example.com', true);
$controller = new UpgradeController(true);
$this->migrationFiles(1);
$_GET['migration_index'] = 1;
$results = $controller->go();
$obj = json_decode($results);
$this->assertTrue($obj->processed);
$updated_file = file_get_contents($this->test_migrations[0]);
$updated_file = preg_replace('/\-\-.*/','', $updated_file);
$updated_file = str_replace('tu_', 'new_prefix_', $updated_file);
$this->debug($obj->sql);
$this->assertEqual($obj->sql, $updated_file);
$this->assertFalse(strrpos($obj->sql, 'tu_'));
$sql = "show tables like 'new_prefix_test1'";
$stmt = $this->pdo->query($sql);
$data = $stmt->fetch();
$this->assertEqual($data[0], 'new_prefix_test1');
$sql = 'select * from new_prefix_test1';
$stmt = $this->pdo->query($sql);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$this->assertEqual(count($data), 3);
}
private function migrationFiles($count = 1) {
$config = Config::getInstance();
$app_version = $config->getValue('THINKUP_VERSION');
$migration_version = $app_version - 1;
$migration_test1 = $this->migrations_test_dir . $this->migrations_file1;
$migration1 = $this->migrations_dir
. '2010-09-17_v' . $migration_version . '.sql.migration';
copy($migration_test1, $migration1);
$this->test_migrations[] = $migration1;
if ($count == 2) {
$migration_test2 = $this->migrations_test_dir . $this->migrations_file2;
$migration_version--;
$migration_version += 0.12;
$migration_version .= 'beta';
$migration2 = $this->migrations_dir
. '2010-09-16_v' . $migration_version . '.sql.migration';
copy($migration_test2, $migration2);
$this->test_migrations[] = $migration2;
}
}
private function newMigrationFiles($name, $old = false, $add_sql = false, $no_version = false, $date = false) {
$config = Config::getInstance();
$app_version = $config->getValue('THINKUP_VERSION');
if (! $date) {
$date = '2011-09-21';
}
$migration_version = $app_version;
if ($old == false) {
$migration_version = $app_version;
} else {
$migration_version = $app_version - 10;
}
$migration_test1 = $this->migrations_test_dir . $this->migrations_file1;
if ($no_version) {
$migration_test1 = $this->migrations_test_dir . $this->migrations_file2;
}
$migration1 = $this->migrations_dir . $date . '_' . $name;
if (! $no_version) {
$migration1 .= '_v' . $migration_version;
}
$migration1 .= '.sql';
if (file_exists($migration1)) {
unlink($migration1);
}
copy($migration_test1, $migration1);
if ($add_sql) {
$msql = file_get_contents($migration1);
$msql .= "\n" . $add_sql;
file_put_contents($migration1, $msql);
}
$this->test_migrations[] = $migration1;
}
private function newMigrationFiles2($name, $date = false, $bad_sql = false) {
$config = Config::getInstance();
$app_version = $config->getValue('THINKUP_VERSION');
if (! $date) {
$date = '2011-09-21';
}
$migration_version = $app_version;
$migration_version = $app_version;
$migration_test3 = $this->migrations_test_dir . $this->migrations_file3;
$migration3 = $this->migrations_dir . $date . '_' . $name;
$migration3 .= '_v' . $migration_version;
$migration3 .= '.sql';
if (file_exists($migration3)) {
unlink($migration3);
}
copy($migration_test3, $migration3);
if ($bad_sql) {
$msql = file_get_contents($migration3);
$msql = preg_replace("/INSERT INTO tu_test3_b16/", 'INSERT INTO tu_test3_b16_badtable', $msql);
file_put_contents($migration3, $msql);
}
$this->test_migrations[] = $migration3;
}
}