Skip to content

Commit

Permalink
#43 RemoveKeywords, some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
j3nsch committed Aug 29, 2023
1 parent f066bbd commit 9d21aec
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 5 deletions.
78 changes: 73 additions & 5 deletions src/Rules/RemoveKeyword.php → src/Rules/RemoveKeywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@

use Opus\Common\DocumentInterface;

use function array_map;
use function is_array;
use function mb_split;

/**
* TODO logging, error handling
* TODO support list of keywords (or should that be a RemoveKeywords rule?)
*/
class RemoveKeyword extends AbstractImportRule
class RemoveKeywords extends AbstractImportRule
{
/** @var string */
private $keyword;
/** @var string[] */
private $keywords;

/** @var string */
private $keywordType;
Expand All @@ -55,8 +59,72 @@ public function apply($document)
{
$condition = $this->getCondition();
if ($condition === null || $condition->applies($document)) {
// TODO remove keyword (that was matched in condition)
$keyword = 'TODO';
$keywords = $this->getKeywords();
if ($keywords !== null) {
$caseSensitive = $this->isCaseSensitive();
$keywordType = $this->getKeywordType();
foreach ($this->getKeywords() as $keyword) {
$document->removeSubject($keyword, $keywordType, $caseSensitive);
}
}
}
}

/**
* @return string[]
*/
public function getKeywords()
{
return $this->keywords;
}

/**
* @param string|string[] $keywords
* @return $this
*/
public function setKeywords($keywords)
{
if (is_array($keywords) || $keywords === null) {
$this->keywords = $keywords;
} else {
$this->keywords = array_map('trim', mb_split(',', $keywords));
}
return $this;
}

/**
* @return null|string
*/
public function getKeywordType()
{
return $this->keywordType;
}

/**
* @param null|string $type
* @return $this
*/
public function setKeywordType($type)
{
$this->keywordType = $type;
return $this;
}

/**
* @return bool
*/
public function isCaseSensitive()
{
return $this->caseSensitive;
}

/**
* @param bool $caseSensitive
* @return $this
*/
public function setCaseSensitive($caseSensitive)
{
$this->caseSensitive = $caseSensitive;
return $this;
}
}
137 changes: 137 additions & 0 deletions test/Rules/RemoveKeywordsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?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\Import\Rules;

use Opus\Common\Document;
use Opus\Import\ImportRules;
use Opus\Import\Rules\RemoveKeywords;
use OpusTest\Import\TestAsset\TestCase;

class RemoveKeywordsTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

$this->adjustConfiguration([
'sword' => ['enableImportRules' => true],
]);
}

public function testRemoveKeywordUsingCondition()
{
$this->adjustConfiguration([
'import' => [
'rules' => [
'keyword1' => [
'type' => 'RemoveKeywords',
'condition' => [
'keyword' => [
'value' => 'RemoveMe',
'remove' => true,
],
],
],
],
'rulesConfigFile' => null,
],
]);

$doc = Document::new();
$keyword = $doc->addSubject();
$keyword->setValue('RemoveMe');
$keyword->setType('uncontrolled');
$keyword->setLanguage('eng');

$rules = new ImportRules();
$rules->init();

$rules->apply($doc);

$keywords = $doc->getSubject();

$this->assertCount(0, $keywords);
}

public function testSetKeywordsSingleValue()
{
$rule = new RemoveKeywords();
}

public function testSetKeywordsNull()
{
$rule = new RemoveKeywords();
}

public function testSetKeywordsCsv()
{
$rule = new RemoveKeywords();
}

public function testSetKeywordsArray()
{
$rule = new RemoveKeywords();
}

public function testRemoveKeyword()
{
$doc = Document::new();
$keyword = $doc->addSubject();
$keyword->setValue('RemoveMe');
$keyword->setType('uncontrolled');
$keyword->setLanguage('eng');

$rule = new RemoveKeywords();
$rule->setKeywords('RemoveMe');

$rule->apply($doc);

$keywords = $doc->getSubject();

$this->assertCount(0, $keywords);
}

public function testRemoveMultipleKeywords()
{
$rule = new RemoveKeywords();
}

public function testRemoveKeywordsUsingType()
{
$rule = new RemoveKeywords();
}

public function testRemoveKeywordsCaseSensitive()
{
$rule = new RemoveKeywords();
}
}

0 comments on commit 9d21aec

Please sign in to comment.