Skip to content
Extraits de code Groupes Projets
Valider cc9fc7a5 rédigé par Thomas Steur's avatar Thomas Steur Validation de Matthieu Aubry
Parcourir les fichiers

make sure to always trigger site event when creating a new site instance (#11001)

parent e72541e5
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -55,20 +55,33 @@ class Site ...@@ -55,20 +55,33 @@ class Site
*/ */
protected static $infoSites = array(); protected static $infoSites = array();
private $site = array();
/** /**
* Constructor. * Constructor.
* *
* @param int $idsite The ID of the site we want data for. * @param int $idsite The ID of the site we want data for.
* @throws UnexpectedWebsiteFoundException
*/ */
public function __construct($idsite) public function __construct($idsite)
{ {
$this->id = (int)$idsite; $this->id = (int) $idsite;
if (!isset(self::$infoSites[$this->id])) {
if (!empty(self::$infoSites[$this->id])) {
$site = self::$infoSites[$this->id];
} else {
$site = API::getInstance()->getSiteFromId($this->id); $site = API::getInstance()->getSiteFromId($this->id);
$sites = array(&$site);
self::triggerSetSitesEvent($sites); if (empty($site)) {
self::setSiteFromArray($this->id, $site); throw new UnexpectedWebsiteFoundException('The requested website id = ' . (int)$this->id . ' couldn\'t be found');
}
} }
$sites = array(&$site);
self::triggerSetSitesEvent($sites);
self::setSiteFromArray($this->id, $site);
$this->site = $site;
} }
/** /**
...@@ -251,19 +264,11 @@ class Site ...@@ -251,19 +264,11 @@ class Site
*/ */
protected function get($name) protected function get($name)
{ {
if (!isset(self::$infoSites[$this->id])) { if (isset($this->site[$name])) {
$site = API::getInstance()->getSiteFromId($this->id); return $this->site[$name];
if (empty($site)) {
throw new UnexpectedWebsiteFoundException('The requested website id = ' . (int)$this->id . ' couldn\'t be found');
}
self::setSiteFromArray($this->id, $site);
} }
if (!isset(self::$infoSites[$this->id][$name])) {
throw new Exception("The property $name could not be found on the website ID " . (int)$this->id); throw new Exception("The property $name could not be found on the website ID " . (int)$this->id);
}
return self::$infoSites[$this->id][$name];
} }
/** /**
......
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration;
use Piwik\Piwik;
use Piwik\Plugins\SitesManager\API;
use Piwik\Site;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
/**
* @group Core
*/
class SiteTest extends IntegrationTestCase
{
private $idSite;
public $siteAppendix = ' foo';
public function setUp()
{
parent::setUp();
$this->idSite = Fixture::createWebsite('2014-01-02 03:04:05');
$self = $this;
Piwik::addAction('Site.setSites', function (&$sites) use ($self) {
foreach ($sites as &$site) {
if (strpos($site['name'], $self->siteAppendix) !== 0) {
$site['name'] .= $self->siteAppendix;
}
}
});
}
/**
* @expectedException \Piwik\Exception\UnexpectedWebsiteFoundException
* @expectedExceptionMessage An unexpected website was found in the request
*/
public function test_constructor_throwsException_ifSiteDoesNotExist()
{
$this->makeSite(9999);
}
public function test_constructor_enrichesSite()
{
$site = $this->makeSite($this->idSite);
$this->assertSame('Piwik test' . $this->siteAppendix, $site->getName());
}
public function test_construct_enrichesSiteEvenIfSiteWasSetToCachePreviously()
{
$site = API::getInstance()->getSiteFromId($this->idSite);
Site::setSiteFromArray($this->idSite, $site);
$site = $this->makeSite($this->idSite);
$this->assertSame('Piwik test' . $this->siteAppendix, $site->getName());
}
public function test_construct_whenRemovingSiteFromGlobalSitesArray_TheObjectItselfStillworks()
{
$site = $this->makeSite($this->idSite);
$this->assertSame('Piwik test' . $this->siteAppendix, $site->getName());
Site::clearCache();
$this->assertSame('Piwik test' . $this->siteAppendix, $site->getName());
$this->assertSame(array(), Site::getSites()); // make sure data was not fetched again
}
private function makeSite($idSite)
{
return new Site($idSite);
}
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter