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

Search content privacy by folder tests #95

Draft
wants to merge 1 commit into
base: 3
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions _config/extensions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ SilverStripe\CMS\Model\SiteTree:
extensions:
SearchServiceExtension: SilverStripe\SearchService\Extensions\SearchServiceExtension
SiteTreeHierarchyExtension: SilverStripe\SearchService\Extensions\SiteTreeHierarchyExtension
SilverStripe\Assets\File:
extensions:
SearchServiceExtension: SilverStripe\SearchService\Extensions\SearchServiceExtension
74 changes: 74 additions & 0 deletions tests/Service/FolderPrivacyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace SilverStripe\SearchService\Tests\Service;

use SilverStripe\Forms\Form;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Image;
use SilverStripe\Forms\TabSet;
use SilverStripe\Assets\Folder;
use SilverStripe\Security\Group;
use SilverStripe\Forms\FieldList;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\SearchService\DataObject\DataObjectDocument;
use SilverStripe\SearchService\Extensions\SearchFormFactoryExtension;


class FolderPrivacyTest extends SapphireTest
{

protected static $fixture_file = [ // phpcs:ignore
'../fixtures.yml',
'../pages.yml',
];

public function testDefaultFolderFileShowInSearch(): void
{
$folder = $this->objFromFixture(Folder::class, 'folder');
// Add file to folder
$file = $this->objFromFixture(File::class, 'pdf-file');
$file->ParentID = $folder->ID;
$file->write();

$this->assertCount(1, $folder->myChildren());

// File should be indexed
$doc = DataObjectDocument::create($file);
$this->assertTrue($doc->shouldIndex());
}

public function testRestrictedFolderFileShowInSearch(): void
{
$folder = $this->objFromFixture(Folder::class, 'folder');
// Set permission to admin only
$folder->CanViewType = 'OnlyTheseUsers';
$folder->ViewerGroups()->add($this->objFromFixture(Group::class, 'admin-group'));
$folder->write();
$folder->publishRecursive();

// File::add_extension(SearchServiceExtension::class,);

// Add files to folder
$file = $this->objFromFixture(File::class, 'pdf-file');
$file->ParentID = $folder->ID;
$file->write();
$image = $this->objFromFixture(Image::class, 'image');
$image->ParentID = $folder->ID;
$image->write();

$this->assertCount(2, $folder->myChildren());

// Remove permission on folder and check if file is indexed
$folder->CanViewType = 'Inherit';
$folder->ViewerGroups()->removeAll();
$folder->write();

// File should be indexed
$doc1 = DataObjectDocument::create($file);
$this->assertTrue($doc1->shouldIndex());

// Image file should not be indexed
$doc2 = DataObjectDocument::create($image);
$this->assertFalse($doc2->shouldIndex());
}
}
25 changes: 25 additions & 0 deletions tests/fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ SilverStripe\SearchService\Tests\Fake\ImageFake:
URL: '/image-two/'
Tags: =>SilverStripe\SearchService\Tests\Fake\TagFake.one,=>SilverStripe\SearchService\Tests\Fake\TagFake.two

SilverStripe\Security\Group:
admin-group:
Title: Administrators
Code: administrators

SilverStripe\Security\Member:
one:
FirstName: member-one-first
Expand All @@ -24,6 +29,10 @@ SilverStripe\Security\Member:
three:
FirstName: member-three-first
Surname: member-three-last
admin-user:
FirstName: admin-first
Surname: admin-last
Groups: =>SilverStripe\Security\Group.admin-group

SilverStripe\SearchService\Tests\Fake\DataObjectFake:
one:
Expand Down Expand Up @@ -64,3 +73,19 @@ SilverStripe\Subsites\Model\Subsite:
Title: 'Subsite 1'
subsite2:
Title: 'Subsite 2'

SilverStripe\Assets\Image:
image:
Name: 'Image'
Filename: silverstripe-logo.png
PopulateFileFrom: tests/silverstripe-logo.png

SilverStripe\Assets\File:
pdf-file:
Title: Test File
Filename: test-file.pdf
PopulateFileFrom: tests/test-file.pdf

SilverStripe\Assets\Folder:
folder:
Name: 'Folder'
Binary file added tests/silverstripe-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/test-file.pdf
Binary file not shown.