Authentication issue with API call to deleteSite


#1

Hello,

I’m very new to Piwik development, and am having trouble getting the Piwik_SitesManager_API::deleteSite API call to work from outside of a logged-in piwik sesson.

I’m running piwik v 1.7.1, and the failing phpunit test is listed below, followed by the error and stack trace.


<?php

define('PIWIK_INCLUDE_PATH', '/Users/Jill/Sites/piwik/piwik');
define('PIWIK_ENABLE_DISPATCH', false);

require_once PIWIK_INCLUDE_PATH . "/index.php";

class NewSiteTrackEvents extends PHPUnit_Framework_TestCase
{
    public function testCreateAndDeleteSite() {
    
        Piwik_FrontController::getInstance()->init();
        Piwik::setUserIsSuperUser();
        $siteMgr = Piwik_SitesManager_API::getInstance();
        
        // Create site
        $random = Piwik_Common::getRandomString();
        $siteUrl = 'http://' . $random . '.anywhere.com';
        $siteid = $siteMgr->addSite($random, $siteUrl);
        $this->assertGreaterThan(0, $siteid);
        
        // Error:
        // Piwik_Access_NoAccessException: You must be logged in to access this functionnality.
        Piwik::setUserIsSuperUser();
        $siteMgr->deleteSite($siteid);
        
        Piwik::setUserIsSuperUser();
        $this->assertEquals( null, $siteMgr->getSiteFromId( $siteid ) );
    }   
}
   
?>

And the error generated is:


1) NewSiteTrackEvents::testCreateAndDeleteSite
Piwik_Access_NoAccessException: You must be logged in to access this functionnality.

/Users/Jill/Sites/piwik/piwik/core/Piwik.php:1747
/Users/Jill/Sites/piwik/piwik/plugins/PDFReports/API.php:183
/Users/Jill/Sites/piwik/piwik/plugins/PDFReports/PDFReports.php:56
/Users/Jill/Sites/piwik/piwik/libs/Event/Dispatcher.php:284
/Users/Jill/Sites/piwik/piwik/core/PluginsManager.php:609
/Users/Jill/Sites/piwik/piwik/plugins/SitesManager/API.php:537
/Users/jill/CreateDeleteSite.php:25

FAILURES!
Tests: 1, Assertions: 1, Errors: 1.

Any assistance would be appreciated!

Cheers,
Jill