Skip to content

Commit

Permalink
Merge pull request #333 from OPUS4/issue332
Browse files Browse the repository at this point in the history
#332 Functions for checking/removing subjects
  • Loading branch information
j3nsch authored Jul 5, 2023
2 parents daed3f0 + 2a8fd00 commit d4b361c
Show file tree
Hide file tree
Showing 2 changed files with 362 additions and 0 deletions.
60 changes: 60 additions & 0 deletions library/Opus/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -1349,4 +1349,64 @@ public function setLifecycleListener($listener)
{
$this->documentLifecycleListener = $listener;
}

/**
* @param string $value
* @param string|null $type
* @param bool $caseSensitive
* @return bool
*/
public function hasSubject($value, $type = null, $caseSensitive = false)
{
$subjects = $this->getSubject();

foreach($subjects as $subject) {
if ($type !== null && $subject->getType() !== $type) {
continue;
}

if ($caseSensitive) {
if (strcmp($value, $subject->getValue()) === 0) {
return true;
}
} else {
if (strcasecmp($value, $subject->getValue()) === 0) {
return true;
}
}
}

return false;
}

/**
* @param string $value
* @param string|null $type
* @param bool $caseSensitive
*/
public function removeSubject($value, $type = null, $caseSensitive = false)
{
$subjects = $this->getSubject();

$remainingSubjects = [];

foreach($subjects as $subject) {
if ($type !== null && $subject->getType() !== $type) {
$remainingSubjects[] = $subject;
continue;
}

if ($caseSensitive) {
if (strcmp($value, $subject->getValue()) !== 0) {
$remainingSubjects[] = $subject;
}
} else {
if (strcasecmp($value, $subject->getValue()) !== 0) {
$remainingSubjects[] = $subject;
}
}
}

$this->setSubject($remainingSubjects);
}
}
302 changes: 302 additions & 0 deletions tests/Opus/DocumentSubjectsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
<?php

/**
* This file is part of OPUS. The software OPUS has been originally developed
* at the University of Stuttgart with funding from the German Research Net,
* the Federal Department of Higher Education and Research and the Ministry
* of Science, Research and the Arts of the State of Baden-Wuerttemberg.
*
* OPUS 4 is a complete rewrite of the original OPUS software and was developed
* by the Stuttgart University Library, the Library Service Center
* Baden-Wuerttemberg, the Cooperative Library Network Berlin-Brandenburg,
* the Saarland University and State Library, the Saxon State Library -
* Dresden State and University Library, the Bielefeld University Library and
* the University Library of Hamburg University of Technology with funding from
* the German Research Foundation and the European Regional Development Fund.
*
* LICENCE
* OPUS is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the Licence, or any later version.
* OPUS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with OPUS; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright Copyright (c) 2023, OPUS 4 development team
* @license http://www.gnu.org/licenses/gpl.html General Public License
*/

namespace OpusTest;

use Opus\Common\Config;
use Opus\Common\Document;
use Opus\Common\Subject;
use OpusTest\TestAsset\TestCase;

use function strtolower;
use function strtoupper;

/**
* Test cases for class Opus\Document.
*/
class DocumentSubjectsTest extends TestCase
{
/**
* Set up test fixture.
*/
public function setUp(): void
{
// Set up a mock language list.
$list = ['de' => 'Test_Deutsch', 'en' => 'Test_Englisch', 'fr' => 'Test_Französisch'];
Config::getInstance()->setAvailableLanguages($list);

parent::setUp();

$this->clearTables(false);
}

public function tearDown(): void
{
$document = Document::new();
$document->setDefaultPlugins(null);

parent::tearDown();
}

public function testHasSubject()
{
$doc = Document::new();

$subjectValue = 'OA-Green';

$doc->addSubject(Subject::new()
->setValue($subjectValue)
->setLanguage('eng')
->setType('uncontrolled'));

$this->assertCount(1, $doc->getSubject());

$this->assertTrue($doc->hasSubject($subjectValue));
$this->assertTrue($doc->hasSubject(strtolower($subjectValue)));
$this->assertTrue($doc->hasSubject(strtoupper($subjectValue)));
$this->assertFalse($doc->hasSubject('OA_Green'));
}

public function testHasSubjectWithType()
{
$doc = Document::new();

$subjectValue = 'OA-Green';

$doc->addSubject(Subject::new()
->setValue($subjectValue)
->setLanguage('eng')
->setType('uncontrolled'));

$this->assertCount(1, $doc->getSubject());

$this->assertTrue($doc->hasSubject($subjectValue, 'uncontrolled'));
$this->assertFalse($doc->hasSubject($subjectValue, 'swd'));
}

public function testHasSubjectCaseSensitive()
{
$doc = Document::new();

$subjectValue = 'OA-Green';

$doc->addSubject(Subject::new()
->setValue($subjectValue)
->setLanguage('eng')
->setType('uncontrolled'));

$this->assertCount(1, $doc->getSubject());

$this->assertTrue($doc->hasSubject($subjectValue));
$this->assertFalse($doc->hasSubject(strtolower($subjectValue), null, true));
$this->assertFalse($doc->hasSubject(strtoupper($subjectValue), null, true));
}

public function testHasSubjectWithTypeCaseSensitive()
{
$doc = Document::new();

$subjectValue = 'OA-Green';

$doc->addSubject(Subject::new()
->setValue($subjectValue)
->setLanguage('eng')
->setType('uncontrolled'));

$this->assertCount(1, $doc->getSubject());

$this->assertTrue($doc->hasSubject($subjectValue, 'uncontrolled'));
$this->assertFalse($doc->hasSubject(strtolower($subjectValue), 'uncontrolled', true));
$this->assertFalse($doc->hasSubject(strtoupper($subjectValue), 'uncontrolled', true));
$this->assertFalse($doc->hasSubject($subjectValue, 'swd'));
$this->assertFalse($doc->hasSubject(strtolower($subjectValue), 'swd', true));
$this->assertFalse($doc->hasSubject(strtoupper($subjectValue), 'swd', true));
}

public function testRemoveSubject()
{
$doc = Document::new();

$subjectGreen = 'OA-Green';
$subjectGold = 'OA-Gold';

$doc->addSubject(Subject::new()
->setValue($subjectGreen)
->setLanguage('eng')
->setType('uncontrolled'));

$doc->addSubject(Subject::new()
->setValue($subjectGold)
->setLanguage('eng')
->setType(Subject::TYPE_PSYNDEX));

$this->assertCount(2, $doc->getSubject());

$doc->removeSubject($subjectGold);

$this->assertCount(1, $doc->getSubject());
$this->assertEquals($subjectGreen, $doc->getSubject()[0]->getValue());
}

public function testRemoveSubjectWithType()
{
$doc = Document::new();

$subjectGreen = 'OA-Green';
$subjectGold = 'OA-Gold';

$doc->addSubject(Subject::new()
->setValue($subjectGreen)
->setLanguage('eng')
->setType('uncontrolled'));

$doc->addSubject(Subject::new()
->setValue($subjectGold)
->setLanguage('eng')
->setType(Subject::TYPE_PSYNDEX));

$this->assertCount(2, $doc->getSubject());

$doc->removeSubject($subjectGold, Subject::TYPE_UNCONTROLLED);

$this->assertCount(2, $doc->getSubject());

$doc->removeSubject($subjectGold, Subject::TYPE_PSYNDEX);

$this->assertCount(1, $doc->getSubject());
$this->assertEquals($subjectGreen, $doc->getSubject()[0]->getValue());
}

public function testRemoveSubjectCaseSensitive()
{
$doc = Document::new();

$subjectGreen = 'OA-Green';
$subjectGold = 'OA-Gold';

$doc->addSubject(Subject::new()
->setValue($subjectGreen)
->setLanguage('eng')
->setType('uncontrolled'));

$doc->addSubject(Subject::new()
->setValue($subjectGold)
->setLanguage('eng')
->setType(Subject::TYPE_PSYNDEX));

$this->assertCount(2, $doc->getSubject());

$doc->removeSubject('oa-gold', null, true);

$this->assertCount(2, $doc->getSubject());

$doc->removeSubject('OA-Gold', null, true);

$this->assertCount(1, $doc->getSubject());
$this->assertEquals($subjectGreen, $doc->getSubject()[0]->getValue());
}

public function testRemoveSubjectWithTypeCaseSensitive()
{
$doc = Document::new();

$subjectGreen = 'OA-Green';
$subjectGold = 'OA-Gold';

$doc->addSubject(Subject::new()
->setValue($subjectGreen)
->setLanguage('eng')
->setType('uncontrolled'));

$doc->addSubject(Subject::new()
->setValue($subjectGold)
->setLanguage('eng')
->setType(Subject::TYPE_PSYNDEX));

$this->assertCount(2, $doc->getSubject());

$doc->removeSubject('oa-gold', Subject::TYPE_PSYNDEX, true);

$this->assertCount(2, $doc->getSubject());

$doc->removeSubject('OA-Gold', Subject::TYPE_PSYNDEX, true);

$this->assertCount(1, $doc->getSubject());
$this->assertEquals($subjectGreen, $doc->getSubject()[0]->getValue());
}

public function testRemoveMultipleSubjectsOfDifferentType()
{
$doc = Document::new();

$subjectGreen = 'OA-Green';

$doc->addSubject(Subject::new()
->setValue($subjectGreen)
->setLanguage('eng')
->setType('uncontrolled'));

$doc->addSubject(Subject::new()
->setValue($subjectGreen)
->setLanguage('eng')
->setType(Subject::TYPE_PSYNDEX));

$this->assertCount(2, $doc->getSubject());

$doc->removeSubject($subjectGreen);

$this->assertCount(0, $doc->getSubject());
}

public function testRemoveSameSubjectsForSingleType()
{
$doc = Document::new();

$subjectGreen = 'OA-Green';

$doc->addSubject(Subject::new()
->setValue($subjectGreen)
->setLanguage('eng')
->setType('uncontrolled'));

$doc->addSubject(Subject::new()
->setValue($subjectGreen)
->setLanguage('eng')
->setType(Subject::TYPE_PSYNDEX));

$this->assertCount(2, $doc->getSubject());

$doc->removeSubject($subjectGreen, 'uncontrolled');

$this->assertCount(1, $doc->getSubject());
$this->assertEquals(Subject::TYPE_PSYNDEX, $doc->getSubject()[0]->getType());
}
}

0 comments on commit d4b361c

Please sign in to comment.