Skip to content

Commit

Permalink
Testing XmlSchemesValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
markusweigelt committed Aug 26, 2024
1 parent 2722296 commit 4367dda
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Classes/Validation/XmlSchemesValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(array $configuration)
* @param $value DOMDocument The value to validate
* @return bool True if is valid
*/
public function isSchemeValid(DOMDocument $value): bool
protected function isSchemeValid(DOMDocument $value): bool
{
$xsd = '<?xml version="1.0" encoding="utf-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">';
foreach ($this->schemes as $scheme) {
Expand Down
2 changes: 0 additions & 2 deletions Tests/Unit/Validation/DOMDocumentValidationStackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
*/
class DOMDocumentValidationStackTest extends UnitTestCase
{

public function testValueTypeException(): void
{
$domDocumentValidationStack = new DOMDocumentValidationStack([]);
Expand Down Expand Up @@ -75,5 +74,4 @@ public function testBreakOnError(): void
$result = $domDocumentValidationStack->validate(new DOMDocument());
self::assertCount(2, $result->getErrors());
}

}
61 changes: 58 additions & 3 deletions Tests/Unit/Validation/XmlSchemesValidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,65 @@
<?php

namespace Kitodo\Dlf\Validation;
declare(strict_types=1);

use PHPUnit\Framework\TestCase;
/**
* (c) Kitodo. Key to digital objects e.V. <[email protected]>
*
* This file is part of the Kitodo and TYPO3 projects.
*
* @license GNU General Public License version 3 or later.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

class XmlSchemesValidatorTest extends TestCase

namespace Kitodo\Dlf\Tests\Unit\Validation;

use Kitodo\Dlf\Validation\XmlSchemesValidator;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

/**
* Testing the XmlSchemesValidator class.
*
* @package TYPO3
* @subpackage dlf
*
* @access public
*/
class XmlSchemesValidatorTest extends UnitTestCase
{
const METS = '<mets:mets xmlns:mets="http://www.loc.gov/METS/" xmlns:mods="http://www.loc.gov/mods/v3" ><mets:metsHdr></mets:metsHdr><mets:amdSec></mets:amdSec><mets:fileSec><mets:fileGrp></mets:fileGrp></mets:fileSec><mets:structMap><mets:div></mets:div></mets:structMap></mets:mets>';

const METS_MODS = '<mets:mets xmlns:mets="http://www.loc.gov/METS/" xmlns:mods="http://www.loc.gov/mods/v3" ><mets:metsHdr></mets:metsHdr><mets:dmdSec ID="DMD1"><mets:mdWrap MDTYPE="MODS"><mets:xmlData><mods:mods><mods:titleInfo></mods:titleInfo></mods:mods></mets:xmlData></mets:mdWrap></mets:dmdSec><mets:amdSec></mets:amdSec><mets:fileSec><mets:fileGrp></mets:fileGrp></mets:fileSec><mets:structMap><mets:div></mets:div></mets:structMap></mets:mets>';

public function testValidation(): void
{
$mets = ["namespace" => "http://www.loc.gov/METS/", "schemaLocation" => "http://www.loc.gov/standards/mets/mets.xsd"];
$mods = ["namespace" => "http://www.loc.gov/mods/v3", "schemaLocation" => "http://www.loc.gov/standards/mods/mods.xsd"];
$xmlSchemesValidator = new XmlSchemesValidator([$mets, $mods]);

$domDocument = new \DOMDocument();

Check notice on line 41 in Tests/Unit/Validation/XmlSchemesValidatorTest.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Tests/Unit/Validation/XmlSchemesValidatorTest.php#L41

Missing class import via use statement (line '41', column '28').
// Test with empty document
$result = $xmlSchemesValidator->validate($domDocument);
self::assertCount(1, $result->getErrors());

$domDocument->loadXML(self::METS);
$result = $xmlSchemesValidator->validate($domDocument);
self::assertFalse($result->hasErrors());

$domDocument->loadXML(self::METS_MODS);
$result = $xmlSchemesValidator->validate($domDocument);
self::assertFalse($result->hasErrors());

// Test with wrong mets element
$domDocument->loadXML(str_replace("mets:metsHdr", "mets:Hdr", self::METS));
$result = $xmlSchemesValidator->validate($domDocument);
self::assertTrue($result->hasErrors());

// Test with wrong mods element
$domDocument->loadXML(str_replace("mods:titleInfo", "mods:title", self::METS_MODS));

$result = $xmlSchemesValidator->validate($domDocument);
self::assertTrue($result->hasErrors());
}
}

0 comments on commit 4367dda

Please sign in to comment.