Skip to content

Commit

Permalink
Prevent classes in ignoreList from being used to build Edges
Browse files Browse the repository at this point in the history
  • Loading branch information
Melissa Wu authored and chrispenny committed Oct 25, 2021
1 parent 48e4c07 commit 759a936
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/RelationshipGraph/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DataObject;
use Terraformers\KeysForCache\Models\CacheKey;

class Graph implements Flushable
{
Expand Down Expand Up @@ -183,6 +184,27 @@ private function getRelationType(string $className, string $relation): ?string
return null;
}

/**
* All DataObject classes except those in ignore list
*
* @return array
*/
private function getValidClasses(): array
{
// Relations only exist from data objects
$classes = ClassInfo::getValidSubClasses(DataObject::class);

// Don't build edges for classes in the ignorelist
$ignoreList = Config::forClass(CacheKey::class)->get('ignorelist');

return array_filter(
$classes,
static function ($class) use ($ignoreList) {
return !in_array($class, $ignoreList, true);
},
);
}

private function buildEdges(): void
{
$cache = Injector::inst()->get(self::CACHE_KEY);
Expand All @@ -193,8 +215,8 @@ private function buildEdges(): void
return;
}

// Relations only exist from data objects
$classes = ClassInfo::getValidSubClasses(DataObject::class);
$classes = $this->getValidClasses();

// The Edges that we're about to create
$edges = [];

Expand Down
16 changes: 16 additions & 0 deletions tests/RelationshipGraph/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\SiteConfig\SiteConfig;
use Terraformers\KeysForCache\Models\CacheKey;
use Terraformers\KeysForCache\RelationshipGraph\Edge;
use Terraformers\KeysForCache\RelationshipGraph\Graph;
use Terraformers\KeysForCache\RelationshipGraph\Node;
Expand Down Expand Up @@ -263,6 +264,21 @@ public function testCacheSetOnFlush(): void
$this->assertTrue($cache->has(Graph::CACHE_KEY_GLOBAL_CARES));
}

public function testGetValidClasses(): void
{
$graph = Graph::singleton();
$reflectionClass = new ReflectionClass(Graph::class);
$getValidClasses = $reflectionClass->getMethod('getValidClasses');
$getValidClasses->setAccessible(true);

$validClasses = $getValidClasses->invoke($graph);
$ignoreList = CacheKey::config()->get('ignorelist');
$intersect = array_intersect($validClasses, $ignoreList);

// validClasses shouldn't contain any value from ignore list
$this->assertCount(0, $intersect);
}

protected function tearDown(): void
{
Injector::inst()->get(Graph::CACHE_KEY)->clear();
Expand Down

0 comments on commit 759a936

Please sign in to comment.