Newer
Older
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
* @version $Id$
*/
require_once PIWIK_INCLUDE_PATH . '/libs/PiwikTracker/PiwikTracker.php';
/**
* Base class for Integration tests.
*
* Provides helpers to track data and then call API get* methods to check outputs automatically.
*
*/
abstract class IntegrationTestCase extends PHPUnit_Framework_TestCase
{
/**
* Identifies the last language used in an API/Controller call.
*
* @var string
*/
protected $lastLanguage;
public static function setUpBeforeClass()
try {
Piwik::createConfigObject();
Piwik_Config::getInstance()->setTestEnvironment();
$dbConfig = Piwik_Config::getInstance()->database;
$dbName = $dbConfig['dbname'];
$dbConfig['dbname'] = null;
Piwik::createDatabaseObject($dbConfig);
Piwik::dropDatabase();
Piwik::createDatabase($dbName);
Piwik::disconnectDatabase();
Piwik::createDatabaseObject();
Piwik::createTables();
Piwik::createLogObject();
Piwik_PluginsManager::getInstance()->loadPlugins(array());
} catch(Exception $e) {
self::fail("TEST INITIALIZATION FAILED: " .$e->getMessage());
}
include "DataFiles/SearchEngines.php";
include "DataFiles/Languages.php";
include "DataFiles/Countries.php";
include "DataFiles/Currencies.php";
include "DataFiles/LanguageToCountry.php";
if (self::$widgetTestingLevel != self::NO_WIDGET_TESTING)
{
self::initializeControllerTesting();
}
Piwik::createAccessObject();
Piwik_PostEvent('FrontController.initAuthenticationObject');
// We need to be SU to create websites for tests
Piwik::setUserIsSuperUser();
// Load and install plugins
$pluginsManager = Piwik_PluginsManager::getInstance();
$plugins = Piwik_Config::getInstance()->Plugins['Plugins'];
$pluginsManager->loadPlugins( $plugins );
$pluginsManager->installLoadedPlugins();
$_GET = $_REQUEST = array();
$_SERVER['HTTP_REFERER'] = '';
// Make sure translations are loaded to check messages in English
Piwik_Translate::getInstance()->loadEnglishTranslation();
// List of Modules, or Module.Method that should not be called as part of the XML output compare
// Usually these modules either return random changing data, or are already tested in specific unit tests.
self::setApiNotToCall(self::$defaultApiNotToCall);
self::setApiToCall( array());
if (self::$widgetTestingLevel != self::NO_WIDGET_TESTING)
{
Piwik::setUserIsSuperUser();
// create users for controller testing
$usersApi = Piwik_UsersManager_API::getInstance();
$usersApi->addUser('anonymous', self::DEFAULT_USER_PASSWORD, 'anonymous@anonymous.com');
$usersApi->addUser('test_view', self::DEFAULT_USER_PASSWORD, 'view@view.com');
$usersApi->addUser('test_admin', self::DEFAULT_USER_PASSWORD, 'admin@admin.com');
// disable shuffling of tag cloud visualization so output is consistent
Piwik_Visualization_Cloud::$debugDisableShuffle = true;
}
}
public static function tearDownAfterClass()
try {
$plugins = Piwik_PluginsManager::getInstance()->getLoadedPlugins();
foreach($plugins AS $plugin) {
$plugin->uninstall();
}
Piwik_PluginsManager::getInstance()->unloadPlugins();
} catch (Exception $e) {}
Piwik::dropDatabase();
Piwik_DataTable_Manager::getInstance()->deleteAll();
Piwik_Option::getInstance()->clearCache();
Piwik_Site::clearCache();
Piwik_Common::deleteTrackerCache();
Piwik_Config::getInstance()->clear();
Piwik_TablePartitioning::$tablesAlreadyInstalled = null;
Zend_Registry::_unsetInstance();
$_GET = $_REQUEST = array();
Piwik_Translate::getInstance()->unloadEnglishTranslation();
// re-enable tag cloud shuffling
Piwik_Visualization_Cloud::$debugDisableShuffle = true;
}
public function setUp()
{
}
public function tearDown()
{
}
protected static $apiToCall = array();
protected static $apiNotToCall = array();
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
public static $defaultApiNotToCall = array(
'LanguagesManager',
'DBStats',
'UsersManager',
'SitesManager',
'ExampleUI',
'Live',
'SEO',
'ExampleAPI',
'PDFReports',
'API',
'ImageGraph',
);
/**
* Widget testing level constant. If self::$widgetTestingLevel is
* set to this, controller actions will not be tested.
*/
const NO_WIDGET_TESTING = 'none';
/**
* Widget testing level constant. If self::$widgetTestingLevel is
* set to this, controller actions will be checked for non-fatal errors, but
* the output will be ignored.
*/
const CHECK_WIDGET_ERRORS = 'check_errors';
/**
* Widget testing level constant. If self::$widgetTestingLevel is
* set to this, controller actions will be run & their output will be checked with
* expected output files.
*/
const COMPARE_WIDGET_OUTPUT = 'compare_output';
/**
* Determines how much of controller actions are tested (if at all).
*/
static public $widgetTestingLevel = self::NO_WIDGET_TESTING;
/**
* API testing level constant. If self::$apiTestingLevel is
* set to this, API methods will not be tested.
*/
const NO_API_TESTING = 'none';
/**
* API testing level constant. If self::$apiTestingLevel is
* set to this, API methods will be run & their output will be checked with
* expected output files.
*/
const COMPARE_API_OUTPUT = 'compare_output';
/**
* Determines how much testing API methods are subjected to (if any).
*/
static public $apiTestingLevel = self::COMPARE_API_OUTPUT;
const DEFAULT_USER_PASSWORD = 'nopass';
/**
* Forces the test to only call and fetch XML for the specified plugins,
* or exact API methods.
* If not called, all default tests will be executed.
*
* @param array $apiToCall array( 'ExampleAPI', 'Plugin.getData' )
*
* @throws Exception
* @return void
*/
protected static function setApiToCall( $apiToCall )
{
if(func_num_args() != 1)
{
throw new Exception('setApiToCall expects an array');
}
if(!is_array($apiToCall))
{
$apiToCall = array($apiToCall);
}
self::$apiToCall = $apiToCall;
}
/**
* Sets a list of API methods to not call during the test
*
* @param string $apiNotToCall eg. 'ExampleAPI.getPiwikVersion'
*
* @return void
*/
protected static function setApiNotToCall( $apiNotToCall )
{
if(!is_array($apiNotToCall))
{
$apiNotToCall = array($apiNotToCall);
}
self::$apiNotToCall = $apiNotToCall;
}
/**
* Returns a PiwikTracker object that you can then use to track pages or goals.
*
* @param $idSite
* @param $dateTime
* @param boolean $defaultInit If set to true, the tracker object will have default IP, user agent, time, resolution, etc.
*
* @return PiwikTracker
*/
protected static function getTracker($idSite, $dateTime, $defaultInit = true )
$t = new PiwikTracker( $idSite, self::getTrackerUrl());
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
$t->setForceVisitDateTime($dateTime);
if($defaultInit)
{
$t->setIp('156.5.3.2');
// Optional tracking
$t->setUserAgent( "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)");
$t->setBrowserLanguage('fr');
$t->setLocalTime( '12:34:06' );
$t->setResolution( 1024, 768 );
$t->setBrowserHasCookies(true);
$t->setPlugins($flash = true, $java = true, $director = false);
}
return $t;
}
/**
* Creates a website, then sets its creation date to a day earlier than specified dateTime
* Useful to create a website now, but force data to be archived back in the past.
*
* @param string $dateTime eg '2010-01-01 12:34:56'
* @param int $ecommerce
* @param string $siteName
*
* @return int idSite of website created
*/
protected static function createWebsite( $dateTime, $ecommerce = 0, $siteName = 'Piwik test' )
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
{
$idSite = Piwik_SitesManager_API::getInstance()->addSite(
$siteName,
"http://piwik.net/",
$ecommerce,
$ips = null,
$excludedQueryParameters = null,
$timezone = null,
$currency = null
);
// Manually set the website creation date to a day earlier than the earliest day we record stats for
Zend_Registry::get('db')->update(Piwik_Common::prefixTable("site"),
array('ts_created' => Piwik_Date::factory($dateTime)->subDay(1)->getDatetime()),
"idsite = $idSite"
);
// Clear the memory Website cache
Piwik_Site::clearCache();
// add access to all test users if doing controller tests
if (self::$widgetTestingLevel != self::NO_WIDGET_TESTING)
{
$usersApi = Piwik_UsersManager_API::getInstance();
$usersApi->setUserAccess('anonymous', 'view', array($idSite));
$usersApi->setUserAccess('test_view', 'view', array($idSite));
$usersApi->setUserAccess('test_admin', 'admin', array($idSite));
}
return $idSite;
}
/**
* Checks that the response is a GIF image as expected.
* Will fail the test if the response is not the expected GIF
*
* @param $response
*/
protected static function checkResponse($response)
{
$trans_gif_64 = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
$expectedResponse = base64_decode($trans_gif_64);
self::assertEquals($expectedResponse, $response, "Expected GIF beacon, got: <br/>\n" . $response ."<br/>\n");
}
/**
* Returns URL to the proxy script, used to ensure piwik.php
* uses the test environment, and allows variable overwriting
*
* @return string
*/
protected static function getTrackerUrl()
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
{
$piwikUrl = Piwik_Url::getCurrentUrlWithoutFileName();
$pathBeforeRoot = 'tests';
// Running from a plugin
if(strpos($piwikUrl, 'plugins/') !== false)
{
$pathBeforeRoot = 'plugins';
}
$piwikUrl = substr($piwikUrl, 0, strpos($piwikUrl, $pathBeforeRoot.'/')) . 'tests/PHPUnit/proxy-piwik.php';
return $piwikUrl;
}
/**
* Initializes parts of Piwik so controller actions can be called & tested.
*/
public static function initializeControllerTesting()
{
static $initialized = false;
if (!$initialized)
{
Zend_Registry::set('timer', new Piwik_Timer);
$pluginsManager = Piwik_PluginsManager::getInstance();
$pluginsToLoad = Piwik_Config::getInstance()->Plugins['Plugins'];
$pluginsManager->loadPlugins( $pluginsToLoad );
$initialized = true;
}
}
public static function processRequestArgs()
{
// set the widget testing level
if (isset($_GET['widgetTestingLevel']))
{
self::setWidgetTestingLevel($_GET['widgetTestingLevel']);
}
// set the API testing level
if (isset($_GET['apiTestingLevel']))
{
self::setApiTestingLevel($_GET['apiTestingLevel']);
}
}
public static function setWidgetTestingLevel($level)
{
if (!$level) return;
if ($level != self::NO_WIDGET_TESTING &&
$level != self::CHECK_WIDGET_ERRORS &&
$level != self::COMPARE_WIDGET_OUTPUT)
{
echo "<p>Invalid option for 'widgetTestingLevel', ignoring.</p>\n";
return;
}
self::$widgetTestingLevel = $level;
}
public function setApiTestingLevel($level)
{
if (!$level) return;
if ($level != self::NO_API_TESTING &&
$level != self::COMPARE_API_OUTPUT)
{
echo "<p>Invalid option for 'apiTestingLevel', ignoring.</p>";
return;
}
self::$apiTestingLevel = $level;
}
/**
* Given a list of default parameters to set, returns the URLs of APIs to call
* If any API was specified in setApiToCall() we ensure only these are tested.
* If any API is set as excluded (see list below) then it will be ignored.
*
* @param array $parametersToSet Parameters to set in api call
* @param array $formats Array of 'format' to fetch from API
* @param array $periods Array of 'period' to query API
* @param bool $setDateLastN If set to true, the 'date' parameter will be rewritten to query instead a range of dates, rather than one period only.
* @param bool|string $language 2 letter language code, defaults to default piwik language
* @param bool|string $segment
*
* @return array of API URLs query strings
*/
protected function generateUrlsApi( $parametersToSet, $formats, $periods, $setDateLastN = false, $language = false, $segment = false )
{
// Get the URLs to query against the API for all functions starting with get*
$skipped = $requestUrls = array();
$apiMetadata = new Piwik_API_DocumentationGenerator;
foreach(Piwik_API_Proxy::getInstance()->getMetadata() as $class => $info)
{
$moduleName = Piwik_API_Proxy::getInstance()->getModuleNameFromClassName($class);
foreach($info as $methodName => $infoMethod)
{
$apiId = $moduleName.'.'.$methodName;
// If Api to test were set, we only test these
if(!empty(self::$apiToCall)
&& in_array($moduleName, self::$apiToCall) === false
&& in_array($apiId, self::$apiToCall) === false)
{
$skipped[] = $apiId;
continue;
}
// Excluded modules from test
elseif(
(strpos($methodName, 'get') !== 0
|| in_array($moduleName, self::$apiNotToCall) === true
|| in_array($apiId, self::$apiNotToCall) === true
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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
|| $methodName == 'getLogoUrl'
|| $methodName == 'getHeaderLogoUrl'
)
)
{
$skipped[] = $apiId;
continue;
}
foreach($periods as $period)
{
$parametersToSet['period'] = $period;
// If date must be a date range, we process this date range by adding 6 periods to it
if($setDateLastN)
{
if(!isset($parametersToSet['dateRewriteBackup']))
{
$parametersToSet['dateRewriteBackup'] = $parametersToSet['date'];
}
$lastCount = (int)$setDateLastN;
if($setDateLastN === true)
{
$lastCount = 6;
}
$firstDate = $parametersToSet['dateRewriteBackup'];
$secondDate = date('Y-m-d', strtotime("+$lastCount " . $period . "s", strtotime($firstDate)));
$parametersToSet['date'] = $firstDate . ',' . $secondDate;
}
// Set response language
if($language !== false)
{
$parametersToSet['language'] = $language;
}
// Generate for each specified format
foreach($formats as $format)
{
$parametersToSet['format'] = $format;
$parametersToSet['hideIdSubDatable'] = 1;
$parametersToSet['serialize'] = 1;
$exampleUrl = $apiMetadata->getExampleUrl($class, $methodName, $parametersToSet);
if($exampleUrl === false)
{
$skipped[] = $apiId;
continue;
}
// Remove the first ? in the query string
$exampleUrl = substr($exampleUrl, 1);
$apiRequestId = $apiId;
if(strpos($exampleUrl, 'period=') !== false)
{
$apiRequestId .= '_' . $period;
}
$apiRequestId .= '.' . $format;
$requestUrls[$apiRequestId] = $exampleUrl;
}
}
}
}
return $requestUrls;
}
/**
* Will call all get* methods on authorized modules,
* force the archiving,
* record output in XML files
* and compare with the expected outputs.
*
* @param string $testName Used to write the output in a file, used as filename prefix
* @param string|array $formats String or array of formats to fetch from API
* @param int|bool $idSite Id site
* @param string|bool $dateTime Date time string of reports to request
* @param array|bool|string $periods String or array of strings of periods (day, week, month, year)
* @param bool $setDateLastN When set to true, 'date' parameter passed to API request will be rewritten to query a range of dates rather than 1 date only
* @param string|bool $language 2 letter language code to request data in
* @param string|bool $segment Custom Segment to query the data for
* @param string|bool $visitorId Only used for Live! API testing
* @param bool $abandonedCarts Only used in Goals API testing
* @param bool $idGoal
* @param bool $apiModule
* @param bool $apiAction
* @param array $otherRequestParameters
*
* @return void
*/
protected function _callGetApiCompareOutput($testName, $formats = 'xml', $idSite = false, $dateTime = false, $periods = false,
$setDateLastN = false, $language = false, $segment = false, $visitorId = false, $abandonedCarts = false,
$idGoal = false, $apiModule = false, $apiAction = false, $otherRequestParameters = array())
{
if (self::$apiTestingLevel == self::NO_API_TESTING)
{
return;
}
list($pathProcessed, $pathExpected) = $this->getProcessedAndExpectedDirs();
if($periods === false)
{
$periods = 'day';
}
if(!is_array($periods))
{
$periods = array($periods);
}
if(!is_array($formats))
{
$formats = array($formats);
}
if(!is_writable($pathProcessed))
{
$this->fail('To run the tests, you need to give write permissions to the following directory (create it if it doesn\'t exist).<code><br/>mkdir '. $pathProcessed.'<br/>chmod 777 '.$pathProcessed.'</code><br/>');
}
$parametersToSet = array(
'idSite' => $idSite,
'date' => $periods == array('range') ? $dateTime : date('Y-m-d', strtotime($dateTime)),
'expanded' => '1',
'piwikUrl' => 'http://example.org/piwik/',
// Used in getKeywordsForPageUrl
'url' => 'http://example.org/store/purchase.htm',
// Used in Actions.getPageUrl, .getDownload, etc.
// tied to Main.test.php doTest_oneVisitorTwoVisits
// will need refactoring when these same API functions are tested in a new function
'downloadUrl' => urlencode('http://piwik.org/path/again/latest.zip?phpsessid=this is ignored when searching'),
'outlinkUrl' => urlencode('http://dev.piwik.org/svn'),
'pageUrl' => urlencode('http://example.org/index.htm?sessionid=this is also ignored by default'),
'pageName' => urlencode(' Checkout / Purchasing... '),
// do not show the millisec timer in response or tests would always fail as value is changing
'showTimer' => 0,
'language' => $language ? $language : 'en',
'abandonedCarts' => $abandonedCarts ? 1 : 0,
'idSites' => $idSite,
);
$parametersToSet = array_merge($parametersToSet, $otherRequestParameters);
if(!empty($visitorId ))
{
$parametersToSet['visitorId'] = $visitorId;
}
if(!empty($apiModule ))
{
$parametersToSet['apiModule'] = $apiModule;
}
if(!empty($apiAction))
{
$parametersToSet['apiAction'] = $apiAction;
}
if(!empty($segment))
{
$parametersToSet['segment'] = $segment;
}
if($idGoal !== false)
{
$parametersToSet['idGoal'] = $idGoal;
}
$requestUrls = $this->generateUrlsApi($parametersToSet, $formats, $periods, $setDateLastN, $language, $segment);
foreach($requestUrls as $apiId => $requestUrl)
{
#echo "\n\n$requestUrl\n\n";
$isLiveMustDeleteDates = strpos($requestUrl, 'Live.getLastVisits') !== false;
$request = new Piwik_API_Request($requestUrl);
list($processedFilePath, $expectedFilePath) = $this->getProcessedAndExpectedPaths($testName, $apiId);
// Cast as string is important. For example when calling
// with format=original, objects or php arrays can be returned.
// we also hide errors to prevent the 'headers already sent' in the ResponseBuilder (which sends Excel headers multiple times eg.)
$response = (string)$request->process();
if($isLiveMustDeleteDates)
{
$response = $this->removeAllLiveDatesFromXml($response);
}
file_put_contents( $processedFilePath, $response );
$expected = $this->loadExpectedFile($expectedFilePath);
if (empty($expected))
{
continue;
}
// @todo This should not vary between systems AFAIK... "idsubdatatable can differ"
$expected = $this->removeXmlElement($expected, 'idsubdatatable',$testNotSmallAfter = false);
$response = $this->removeXmlElement($response, 'idsubdatatable',$testNotSmallAfter = false);
if($isLiveMustDeleteDates)
{
$expected = $this->removeAllLiveDatesFromXml($expected);
}
// If date=lastN the <prettyDate> element will change each day, we remove XML element before comparison
elseif(strpos($dateTime, 'last') !== false
|| strpos($dateTime, 'today') !== false
|| strpos($dateTime, 'now') !== false
)
{
if(strpos($requestUrl, 'API.getProcessedReport') !== false)
{
$expected = $this->removePrettyDateFromXml($expected);
$response = $this->removePrettyDateFromXml($response);
}
// avoid build failure when running just before midnight, generating visits in the future
$expected = $this->removeXmlElement($expected, 'sum_daily_nb_uniq_visitors');
$response = $this->removeXmlElement($response, 'sum_daily_nb_uniq_visitors');
$expected = $this->removeXmlElement($expected, 'nb_visits_converted');
$response = $this->removeXmlElement($response, 'nb_visits_converted');
$expected = $this->removeXmlElement($expected, 'imageGraphUrl');
$response = $this->removeXmlElement($response, 'imageGraphUrl');
}
// is there a better way to test for the current DB type in use?
if(Zend_Registry::get('db') instanceof Piwik_Db_Adapter_Mysqli)
{
// Do not test for TRUNCATE(SUM()) returning .00 on mysqli since this is not working
// http://bugs.php.net/bug.php?id=54508
$expected = str_replace('.00</revenue>', '</revenue>', $expected);
$response = str_replace('.00</revenue>', '</revenue>', $response);
$expected = str_replace('.1</revenue>', '</revenue>', $expected);
$expected = str_replace('.11</revenue>', '</revenue>', $expected);
$response = str_replace('.11</revenue>', '</revenue>', $response);
$response = str_replace('.1</revenue>', '</revenue>', $response);
}
if(strpos($requestUrl, 'format=xml') !== false) {
$this->assertXmlStringEqualsXmlString($expected, $response, "Differences with expected in: $processedFilePath %s ");
} else {
$this->assertEquals($expected, $response, "Differences with expected in: $processedFilePath %s ");
}
if(trim($response) == trim($expected))
{
file_put_contents( $processedFilePath, $response );
}
}
}
/**
* Calls a set of controller actions & either checks the result against
* expected output or just checks if errors occurred when called.
* The behavior of this function can be modified by setting
* self::$widgetTestingLevel (or $testingLevelOverride):
* <ul>
* <li>If set to <b>NO_WIDGET_TESTING</b> this function simply returns.<li>
* <li>If set to <b>CHECK_WIDGET_ERRORS</b> controller actions are called &
* this function will just check for errors.</li>
* <li>If set to <b>COMPARE_WIDGET_OUTPUT</b> controller actions are
* called & the output is checked against expected output.</li>
* </ul>
*
* @param string $testName Unique name of this test group. Expected/processed
* file names use this as a prefix.
* @param array $actions Array of controller actions to call. Each element
* must be in the following format: 'Controller.action'
* @param array $requestParameters The request parameters to set.
* @param array $userTypes The user types to test the controller with. Can contain
* these values: 'anonymous', 'view', 'admin', 'superuser'.
* Defaults to all four.
* @param int $testingLevelOverride Overrides self::$widgetTestingLevel.
*/
public function callWidgetsCompareOutput(
$testName, $actions, $requestParameters, $userTypes = null, $testingLevelOverride = null)
{
// deal with the testing level
if (self::$widgetTestingLevel == self::NO_WIDGET_TESTING)
{
return;
}
if (is_null($testingLevelOverride))
{
$testingLevelOverride = self::$widgetTestingLevel;
}
// process $userTypes argument
if (!$userTypes)
{
$userTypes = array('anonymous', 'view', 'admin', 'superuser');
}
else if (!is_array($userTypes))
{
$userTypes = array($userTypes);
}
$oldGet = $_GET;
// get all testable controller actions if necessary
$actionParams = array();
if ($actions == 'all')
{
// Goals.addWidgets requires idSite to be set
$_GET['idSite'] = isset($requestParameters['idSite']) ? $requestParameters['idSite'] : '0';
list($actions, $actionParams) = $this->findAllWidgets();
$_GET = $oldGet;
}
else if (!is_array($actions))
{
$actions = array($actions);
}
// run the tests
foreach ($actions as $controllerAction)
{
$customParams = isset($actionParams[$controllerAction]) ? $actionParams[$controllerAction] : array();
list($controllerName, $actionName) = explode('.', $controllerAction);
foreach ($userTypes as $userType)
{
$this->setUserType($userType);
try
{
// set request parameters
$_GET = array();
foreach ($customParams as $key => $value)
{
$_GET[$key] = $value;
}
foreach ($requestParameters as $key => $value)
{
$_GET[$key] = $value;
}
$_GET['module'] = $controllerName;
$_GET['action'] = $actionName;
if ($testingLevelOverride == self::CHECK_WIDGET_ERRORS)
{
$this->errorsOccurredInTest = array();
set_error_handler(array($this, "customErrorHandler"));
}
// call controller action
$response = Piwik_FrontController::getInstance()->fetchDispatch();
list($processedFilePath, $expectedFilePath) = $this->getProcessedAndExpectedPaths(
$testName . '_' . $userType, $controllerAction, 'html');
if ($testingLevelOverride == self::CHECK_WIDGET_ERRORS)
{
restore_error_handler();
if (!empty($this->errorsOccurredInTest))
{
// write processed (only if there are errors)
file_put_contents($processedFilePath, $response);
$this->fail("PHP Errors occurred in calling controller action '$controllerAction':");
foreach ($this->errorsOccurredInTest as $error)
{
echo " $error<br/>\n";
}
}
}
else // check against expected
{
// write raw processed response
file_put_contents($processedFilePath, $response);
// load expected
$expected = $this->loadExpectedFile($expectedFilePath);
if (!$expected)
{
continue;
}
// normalize eol delimeters
$expected = str_replace("\r\n", "\n", $expected);
$response = str_replace("\r\n", "\n", $response);
// check against expected
$passed = $this->assertEquals(trim($expected), trim($response),
"<br/>\nDifferences with expected in: $processedFilePath %s ");
if (!$passed)
{
var_dump('ERROR FOR ' . $controllerAction . ' -- FETCHED RESPONSE, then EXPECTED RESPONSE - ');
echo "<br/>\n";
var_dump(htmlspecialchars($response));
echo "<br/>\n";
var_dump(htmlspecialchars($expected));
echo "<br/>\n";
}
}
}
catch (Exception $e)
{
$this->fail("EXCEPTION THROWN IN $controllerAction: ".$e->getTraceAsString());
}
}
}
// reset $_GET to old values
$_GET = array();
foreach ($oldGet as $key => $value)
{
$_GET[$key] = $value;
}
// set user type
$this->setUserType('superuser');
}
/**
* Sets the access privilegs of the current user to the specified user type.
*
* @param $userType string Can be 'superuser', 'admin', 'view' or 'anonymous'.
*/
protected function setUserType( $userType )
{
if ($userType == 'superuser')
{
$code = Piwik_Auth_Result::SUCCESS_SUPERUSER_AUTH_CODE;
$login = 'superUserLogin';
}
else
{
$code = 0;
$login = $userType;
if ($login != 'anonymous')
{
$login = 'test_' . $login;
}
}
$authResultObj = new Piwik_Auth_Result($code, $login, 'dummyTokenAuth');
$authObj = new MockPiwik_Auth();
$authObj->setReturnValue('getName', 'Login');
$authObj->setReturnValue('authenticate', $authResultObj);
Zend_Registry::get('access')->reloadAccess($authObj);
}
/**
* Set of messages for errors that occurred during the invocation of a
* controller action. If not empty, there was an error in the controller.
*/
private $errorsOccurredInTest = array();
/**
* A custom error handler used with <code>set_error_handler</code>. If
* an error occurs, a message describing it is saved in an array.
*
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
*
* @return void
*/
public function customErrorHandler($errno, $errstr, $errfile, $errline)
{
if (strpos(strtolower($errstr), 'cannot modify header information - headers already sent')) // HACK
{
$this->errorsOccurredInTest[] = "$errfile($errline): - $errstr";
}
}
/**
* Returns a list of all available widgets.
*/
protected function findAllWidgets()
{
$widgetList = Piwik_GetWidgetsList();
$actions = array();
$customParams = array();
foreach($widgetList as $widgetCategory => $widgets)
{
foreach($widgets as $widgetInfo)
{
$module = $widgetInfo['parameters']['module'];
$moduleAction = $widgetInfo['parameters']['action'];
$wholeAction = "$module.$moduleAction";
// FIXME: can't test Referers.getKeywordsForPage since it tries to make a request to
// localhost w/ the wrong url. Piwik_Url::getCurrentUrlWithoutFileName
// returns /tests/integration/?... when used within a test.
if ($wholeAction == "Referers.getKeywordsForPage")
{
continue;
}
// rss widgets depends on feedburner URL. don't test the widget just in case
// feedburner is down.
if ($module == "ExampleRssWidget"
|| $module == "ExampleFeedburner")
{
continue;
}
unset($widgetInfo['parameters']['module']);
unset($widgetInfo['parameters']['action']);
$actions[] = $wholeAction;
$customParams[$wholeAction] = $widgetInfo['parameters'];
}
}
return array($actions, $customParams);
}
protected function removeAllLiveDatesFromXml($input)
{
$toRemove = array(
'serverDate',
'firstActionTimestamp',
'lastActionTimestamp',
'lastActionDateTime',
'serverTimestamp',
'serverTimePretty',
'serverDatePretty',
'serverDatePrettyFirstAction',
'serverTimePrettyFirstAction',
'goalTimePretty',
'serverTimePretty',
'visitorId'
);
foreach($toRemove as $xml) {
$input = $this->removeXmlElement($input, $xml);
}
return $input;
}
protected function removePrettyDateFromXml($input)
{
return $this->removeXmlElement($input, 'prettyDate');
}
protected function removeXmlElement($input, $xmlElement, $testNotSmallAfter = true)
{
$input = preg_replace('/(<'.$xmlElement.'>.+?<\/'.$xmlElement.'>)/', '', $input);
//check we didn't delete the whole string
if($testNotSmallAfter)
{
$this->assertTrue(strlen($input) > 100);
}
return $input;
}
private function getProcessedAndExpectedDirs()
{
$path = $this->getPathToTestDirectory();
return array($path . '/processed/', $path . '/expected/');
}