Skip to content
Extraits de code Groupes Projets
AutoSuggestAPITest.php 4,97 ko
Newer Older
mattab's avatar
mattab a validé
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link    http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
mattab's avatar
mattab a validé

/**
 * testing a the auto suggest API for all known segments
 */
class Test_Piwik_Integration_AutoSuggestAPITest extends IntegrationTestCase
{
    public static $fixture = null; // initialized below class definition

    protected static $processed = 0;
    protected static $skipped = array();

    public static function tearDownAfterClass()
    {
        parent::tearDownAfterClass();
    }

    /**
     * @dataProvider getApiForTesting
     * @group        Integration
     */
    public function testApi($api, $params)
    {
        $this->runApiTests($api, $params);
    }


    public function getApiForTesting()
    {
        // we will test all segments from all plugins
        self::loadAllPlugins();

        $idSite = self::$fixture->idSite;
        $apiForTesting = array();

        $segments = \Piwik\Plugins\API\API::getInstance()->getSegmentsMetadata(self::$fixture->idSite);
mattab's avatar
mattab a validé
        foreach ($segments as $segment) {
mattab's avatar
mattab a validé
            $apiForTesting[] = $this->getApiForTestingForSegment($idSite, $segment['segment']);
        }

        $apiForTesting[] = array('Live.getLastVisitsDetails',
mattab's avatar
mattab a validé
                                 array('idSite' => $idSite,
                                       'date'   => date('Y-m-d', strtotime(self::$fixture->dateTime)),
mattab's avatar
mattab a validé
                                       'period' => 'year'));
        return $apiForTesting;
    }


    /**
     * @param $idSite
     * @param $segment
     * @return array
     */
    protected function getApiForTestingForSegment($idSite, $segment)
    {
        return array('API.getSuggestedValuesForSegment',
                     array('idSite'                 => $idSite,
                           'testSuffix'             => '_' . $segment,
                           'otherRequestParameters' => array('segmentName' => $segment)));
    }

    /**
     * @depends      testApi
     * @dataProvider getAnotherApiForTesting
     * @group        Integration
     */
    public function testAnotherApi($api, $params)
    {
        // Get the top segment value
mattab's avatar
mattab a validé
            'method=API.getSuggestedValuesForSegment'
                . '&segmentName=' . $params['segmentToComplete']
                . '&idSite=' . $params['idSite']
                . '&format=php&serialize=0'
        );
        $response = $request->process();
        $this->checkRequestResponse($response);
mattab's avatar
mattab a validé
        $topSegmentValue = @$response[0];

mattab's avatar
mattab a validé
        if ($topSegmentValue !== false && !is_null($topSegmentValue)) {
mattab's avatar
mattab a validé
            // Now build the segment request
            $segmentValue = rawurlencode(html_entity_decode($topSegmentValue));
            $params['segment'] = $params['segmentToComplete'] . '==' . $segmentValue;
            unset($params['segmentToComplete']);
            $this->runApiTests($api, $params);
            self::$processed++;
        } else {
            self::$skipped[] = $params['segmentToComplete'];
        }

    }

    public function getAnotherApiForTesting()
    {
        $apiForTesting = array();
        $segments = \Piwik\Plugins\API\API::getInstance()->getSegmentsMetadata(self::$fixture->idSite);
mattab's avatar
mattab a validé
        foreach ($segments as $segment) {
            $apiForTesting[] = array('VisitsSummary.get',
                                     array('idSite'            => self::$fixture->idSite,
                                           'date'              => date("Y-m-d", strtotime(self::$fixture->dateTime)) . ',today',
                                           'period'            => 'range',
                                           'testSuffix'        => '_' . $segment['segment'],
                                           'segmentToComplete' => $segment['segment']));
mattab's avatar
mattab a validé
        }
        return $apiForTesting;
    }

    /**
mattab's avatar
mattab a validé
     * @depends      testAnotherApi
     */
    public function testCheckOtherTestsWereComplete()
    {
        // Check that only a few haven't been tested specifically (these are all custom variables slots since we only test slot 1, 2, 5 (see the fixture))
mattab's avatar
mattab a validé
        $maximumSegmentsToSkip = 11;
        $this->assertTrue(count(self::$skipped) <= $maximumSegmentsToSkip, 'SKIPPED ' . count(self::$skipped) . ' segments --> some segments had no "auto-suggested values"
mattab's avatar
mattab a validé
            but we should try and test the autosuggest for all new segments. Segments skipped were: ' . implode(', ', self::$skipped));

        // and check that most others have been tested
mattab's avatar
mattab a validé
        $minimumSegmentsToTest = 46;
mattab's avatar
mattab a validé
        $this->assertTrue(self::$processed >= $minimumSegmentsToTest, 'PROCESSED ' . self::$processed . ' segments --> it seems some segments "auto-suggested values" haven\'t been tested as we were expecting');
mattab's avatar
mattab a validé
    }
}

Test_Piwik_Integration_AutoSuggestAPITest::$fixture = new Test_Piwik_Fixture_ManyVisitsWithGeoIP();
Test_Piwik_Integration_AutoSuggestAPITest::$fixture->dateTime = Date::yesterday()->subDay(30)->getDatetime();