Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test ProjectConfig #1786

Merged
merged 2 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lizmap/modules/lizmap/lib/Project/ProjectConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function unsetProperty($propName, $propName2 = '', $propName3 = '')
public function findLayerByAnyName($name)
{
// name null or empty string
if ($name == null || empty($name)) {
if ($name == null || empty($name) || !isset($this->cfgContent->layers)) {
return null;
}

Expand Down Expand Up @@ -311,7 +311,7 @@ public function findLayerByTypeName($typeName)

public function getEditionLayerByName($name)
{
$editionLayers = $this->cfgContent->editionLayers;
$editionLayers = $this->editionLayers;
if ($editionLayers && property_exists($editionLayers, $name)) {
return $editionLayers->{$name};
}
Expand All @@ -326,7 +326,10 @@ public function getEditionLayerByName($name)
*/
public function getEditionLayerByLayerId($layerId)
{
$editionLayers = $this->cfgContent->editionLayers;
$editionLayers = $this->editionLayers;
if (!$editionLayers) {
return null;
}
foreach ($editionLayers as $layer) {
if (!property_exists($layer, 'layerId')) {
continue;
Expand Down
150 changes: 150 additions & 0 deletions tests/units/classes/Project/ProjectConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

use Lizmap\Project;

/**
* @internal
* @coversNothing
*/
class projectConfigTest extends PHPUnit_Framework_TestCase
{
public function getConstructData()
{
$file = __DIR__.'/Ressources/events.qgs.cfg';

return array(
array(null, json_decode(file_get_contents($file))),
array(array('cfgContent' => json_decode(file_get_contents($file))), json_decode(file_get_contents($file))),
);
}

/**
* @dataProvider getConstructData
*
* @param mixed $data
* @param mixed $expectedData
*/
public function testConstruct($data, $expectedData)
{
$file = __DIR__.'/Ressources/events.qgs.cfg';
$testCfg = new Project\ProjectConfig($file, $data);
$this->assertEquals($expectedData, $testCfg->getData());
}

public function testConstructCache()
{
$file = __DIR__.'/Ressources/events.qgs.cfg';
$json = json_decode(file_get_contents($file));
$data = array('cfgContent' => $json);
foreach ($json as $key => $prop) {
$data[$key] = $json->{$key};
}
$cachedProperties = array('layersOrder', 'locateByLayer', 'formFilterLayers', 'editionLayers',
'attributeLayers', 'cfgContent', 'options', 'layers', );
$testCfg = new Project\ProjectConfig($file, $data);
foreach ($cachedProperties as $prop) {
if (array_key_exists($prop, $data)) {
$this->assertEquals($data[$prop], $testCfg->getProperty($prop), 'failed Prop = '.$prop);
}
}
}

public function getFindLayerData()
{
$file = __DIR__.'/Ressources/events.qgs.cfg';
$json = json_decode(file_get_contents($file));
$layers = array('cfgContent' => (object) array('layers' => $json->layers));
$layersNull = array('cfgContent' => (object) array('layers' => null));

return array(
array($layers, 'events_4c3b47b8_3939_4c8c_8e91_55bdb13a2101', 'montpellier_events'),
array($layers, 'test_shortname', 'Hidden'),
array($layers, 'Hidden', 'Hidden'),
array($layers, 'osm-test', 'osm-stamen-toner'),
array($layers, 'test', null),
array($layers, null, null),
array($layersNull, 'test', null),
array($layersNull, null, null),
);
}

/**
* @dataProvider getFindLayerData
*
* @param mixed $layers
* @param mixed $key
* @param mixed $layerName
*/
public function testFindLayer($layers, $key, $layerName)
{
$testCfg = new Project\ProjectConfig(null, $layers);
if ($layerName) {
$this->assertSame($testCfg->getProperty('layers')->{$layerName}, $testCfg->findLayerByAnyName($key));
} else {
$this->assertNull($testCfg->findLayerByAnyName($key));
}
}

public function getEditionLayerByNameData()
{
$file = __DIR__.'/Ressources/montpellier.qgs.cfg';
$json = json_decode(file_get_contents($file));
$eLayer = array('editionLayers' => $json->editionLayers);
$eLayerNull = array('editionLayers' => null);

return array(
array($eLayer, 'tramstop'),
array($eLayer, 'tram_stop_work'),
array($eLayerNull, null),
);
}

/**
* @dataProvider getEditionLayerByNameData
*
* @param mixed $eLayers
* @param mixed $name
*/
public function testGetEditionLayerByName($eLayers, $name)
{
$testCfg = new Project\ProjectConfig(null, $eLayers);
if ($name) {
$this->assertSame($testCfg->getProperty('editionLayers')->{$name}, $testCfg->getEditionLayerByName($name));
} else {
$this->assertNull($testCfg->getEditionLayerByName($name));
}
}

public function getEditionLayerByLayerIdData()
{
$file = __DIR__.'/Ressources/montpellier.qgs.cfg';
$json = json_decode(file_get_contents($file));
$eLayer = array('editionLayers' => $json->editionLayers);
$eLayerNull = array('editionLayers' => null);

return array(
array($eLayer, 'edition_line20130409161630329', 'edition_line'),
array($eLayer, 'edition_polygon20130409114333776', 'areas_of_interest'),
array($eLayer, 'null', null),
array($eLayerNull, 'null', null),
array($eLayerNull, null, null),
);
}

/**
* @dataProvider getEditionLayerByLayerIdData
*
* @param mixed $eLayers
* @param mixed $id
* @param mixed $eLayerName
*/
public function testGetEditionLayerByLayerId($eLayers, $id, $eLayerName)
{
$testCfg = new Project\ProjectConfig(null, $eLayers);
if ($eLayerName) {
$this->assertSame($testCfg->getProperty('editionLayers')->{$eLayerName}, $testCfg->getEditionLayerByLayerId($id));
} else {
$this->assertNull($testCfg->getEditionLayerByLayerId($id));
}
}
}
Loading