|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\metastore\Functional\Storage; |
| 4 | + |
| 5 | +use ColinODell\PsrTestLogger\TestLogger; |
| 6 | +use Drupal\Core\Config\ConfigFactoryInterface; |
| 7 | +use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 8 | +use Drupal\Core\File\FileSystem; |
| 9 | +use Drupal\Tests\BrowserTestBase; |
| 10 | +use Drupal\metastore\Storage\Data; |
| 11 | +use Psr\Log\LoggerInterface; |
| 12 | +use org\bovigo\vfs\vfsStream; |
| 13 | + |
| 14 | +/** |
| 15 | + * Test the Data class. |
| 16 | + * |
| 17 | + * Because of the tight coupling and unification of concerns, this is a |
| 18 | + * BrowserTestBase test. A Kernel test would be too complex and fragile. |
| 19 | + * |
| 20 | + * @covers \Drupal\metastore\Storage\Data |
| 21 | + * @coversDefaultClass \Drupal\metastore\Storage\Data |
| 22 | + * |
| 23 | + * @group dkan |
| 24 | + * @group metastore |
| 25 | + * @group functional |
| 26 | + * @group btb |
| 27 | + */ |
| 28 | +class DataTest extends BrowserTestBase { |
| 29 | + |
| 30 | + protected $defaultTheme = 'stark'; |
| 31 | + |
| 32 | + protected static $modules = [ |
| 33 | + 'metastore', |
| 34 | + 'node', |
| 35 | + ]; |
| 36 | + |
| 37 | + /** |
| 38 | + * Test the logging of htmlPurifier(). |
| 39 | + * |
| 40 | + * @covers ::htmlPurifier |
| 41 | + */ |
| 42 | + public function testHtmlPurifierLogging() { |
| 43 | + // Set up a read-only temp directory. |
| 44 | + $temp = vfsStream::setup('temp'); |
| 45 | + $temp_dir = vfsStream::newDirectory('mytemp', 0000) |
| 46 | + ->at($temp); |
| 47 | + |
| 48 | + // Tell the file system service to use this temp directory, which will |
| 49 | + // prevent the HTML purifier from writing its cache directory, and it |
| 50 | + // should log this fact. |
| 51 | + $fs = $this->getMockBuilder(FileSystem::class) |
| 52 | + ->disableOriginalConstructor() |
| 53 | + ->onlyMethods(['getTempDirectory']) |
| 54 | + ->getMock(); |
| 55 | + $fs->expects($this->any()) |
| 56 | + ->method('getTempDirectory') |
| 57 | + ->willReturn($temp_dir->url()); |
| 58 | + |
| 59 | + // Let's keep the old filesystem object so we can set it back. |
| 60 | + $old_fs = $this->container->get('file_system'); |
| 61 | + $this->container->set('file_system', $fs); |
| 62 | + |
| 63 | + // Create a Data object with a testable logger. |
| 64 | + $logger = new TestLogger(); |
| 65 | + $data = new StubData( |
| 66 | + 'dataset', |
| 67 | + $this->container->get('entity_type.manager'), |
| 68 | + $this->container->get('config.factory'), |
| 69 | + $logger |
| 70 | + ); |
| 71 | + |
| 72 | + $uuid = '05aea36e-9e24-452e-9cf9-9727ab90c198'; |
| 73 | + |
| 74 | + // Since filterHtml() and htmlPurifier() are private, we call in from |
| 75 | + // store() with crafted data. |
| 76 | + $identifier = $data->store(json_encode((object) [ |
| 77 | + 'identifier' => $uuid, |
| 78 | + 'title' => 'title', |
| 79 | + 'data' => (object) [ |
| 80 | + 'description' => 'purify me', |
| 81 | + ], |
| 82 | + ])); |
| 83 | + |
| 84 | + // We stored a node. |
| 85 | + $this->assertSame($uuid, $identifier); |
| 86 | + // We logged that the cache directory was not created. |
| 87 | + $this->assertTrue( |
| 88 | + $logger->hasErrorThatContains('Failed to create cache directory for HTML purifier') |
| 89 | + ); |
| 90 | + |
| 91 | + // Test can't clean up if we don't set back the old file system service. |
| 92 | + $this->container->set('file_system', $old_fs); |
| 93 | + } |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Stub out the abstract Data class with just enough to make it run. |
| 99 | + * |
| 100 | + * Values are copied from NodeData. |
| 101 | + */ |
| 102 | +class StubData extends Data { |
| 103 | + |
| 104 | + public function __construct(string $schemaId, EntityTypeManagerInterface $entityTypeManager, ConfigFactoryInterface $config_factory, LoggerInterface $loggerChannel) { |
| 105 | + $this->entityType = 'node'; |
| 106 | + $this->bundle = 'data'; |
| 107 | + $this->bundleKey = 'type'; |
| 108 | + $this->labelKey = 'title'; |
| 109 | + $this->schemaIdField = 'field_data_type'; |
| 110 | + $this->metadataField = 'field_json_metadata'; |
| 111 | + parent::__construct($schemaId, $entityTypeManager, $config_factory, $loggerChannel); |
| 112 | + } |
| 113 | + |
| 114 | + public function retrieveContains(string $string, bool $caseSensitive): array { |
| 115 | + return []; |
| 116 | + } |
| 117 | + |
| 118 | + public function retrieveByHash($hash, $schemaId) { |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments