Compare commits

...

4 Commits

7 changed files with 171 additions and 155 deletions

View File

@ -1,46 +0,0 @@
<?php
namespace Drupal\Tests\opencase_defaults\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
/**
* Simple test to ensure that main page loads with module enabled.
*
* @group opencase_defaults
*/
class LoadTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['opencase_defaults'];
/**
* A user with permission to administer site configuration.
*
* @var \Drupal\user\UserInterface
*/
protected $user;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->user = $this->drupalCreateUser(['administer site configuration']);
$this->drupalLogin($this->user);
}
/**
* Tests that the home page loads with a 200 response.
*/
public function testLoad() {
$this->drupalGet(Url::fromRoute('<front>'));
$this->assertSession()->statusCodeEquals(200);
}
}

View File

@ -1,46 +0,0 @@
<?php
namespace Drupal\Tests\opencase_entities\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
/**
* Simple test to ensure that main page loads with module enabled.
*
* @group opencase_entities
*/
class LoadTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['opencase_entities'];
/**
* A user with permission to administer site configuration.
*
* @var \Drupal\user\UserInterface
*/
protected $user;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->user = $this->drupalCreateUser(['administer site configuration']);
$this->drupalLogin($this->user);
}
/**
* Tests that the home page loads with a 200 response.
*/
public function testLoad() {
$this->drupalGet(Url::fromRoute('<front>'));
$this->assertSession()->statusCodeEquals(200);
}
}

View File

@ -2,34 +2,40 @@
namespace Drupal\opencase;
use Drupal;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\opencase\Utils;
final class TimeBasedFieldUpdater {
private string $date_field_to_compare;
private EntityTypeManagerInterface $entityTypeManager;
private string $date_field;
private string $entity_type;
private array $where;
private array $conditions;
private string $date_format;
final public function __construct($entity_type, $where = [], $date_field_to_compare, $date_format = 'Y-m-d')
{
$this->date_field_to_compare = $date_field_to_compare;
$this->where = $where;
final public function __construct(
EntityTypeManagerInterface $entityTypeManager,
Utils $utils,
string $entity_type, string $bundle, string $date_field, string $date_format = 'Y-m-d'
)
{
$this->entityTypeManager = $entityTypeManager;
$this->utils = $utils;
$this->date_field = $date_field;
$this->date_format = $date_format;
$this->entity_type = $entity_type;
$this->bundle = $bundle;
}
final public function update($time_elapsed, $old_values, $new_values): void {
$query = Drupal::entityQuery($this->entity_type);
foreach($this->where as $cond_field=>$cond_value) {
$query->condition($cond_field, $cond_value);
}
foreach($old_values as $old_field=>$old_value) {
$query->condition($old_field, $old_value);
}
$query->condition($this->date_field_to_compare, date($this->date_format, strtotime('-'.$time_elapsed)), "<");
final public function update(array $conditions, string $time_elapsed, array $new_values): void {
$query = $this->entityTypeManager->getStorage($this->entity_type)->getQuery();
$conditions[] = [$this->date_field, date($this->date_format, strtotime('-'.$time_elapsed)), "<"];
$conditions[] = ['type', $this->bundle, '='];
$this->utils->addConditionsToQuery($query, $conditions);
foreach($query->execute() as $id) {
$entity = Drupal::entityTypeManager()->getStorage($this->entity_type)->load($id);
$entity = $this->entityTypeManager->getStorage($this->entity_type)->load($id);
foreach($new_values as $new_field=>$new_value) {
$entity->set($new_field, $new_value);
$entity->$new_field = $new_value;
}
$entity->save();
}

54
src/Utils.php Normal file
View File

@ -0,0 +1,54 @@
<?php declare(strict_types =1);
namespace Drupal\opencase;
use \Drupal;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use RuntimeException;
class Utils {
public function __construct(
EntityTypeManagerInterface $entityTypeManager = null
) {
if ($entityTypeManager == null) {
$entityTypeManager = Drupal::entityTypeManager();
}
$this->entityTypeManager = $entityTypeManager;
}
public function addConditionsToQuery(QueryInterface $query, array $conditions): void {
foreach($conditions as $condition) {
if (sizeof($condition) != 3) throw new RuntimeException('Utils::addConditionsToQuery needs each condition to consist of 3 strings');
$field = $condition[0];
$value = $condition[1];
$operator = $condition[2];
$query->condition($field, $value, $operator);
}
}
/**
* Utility: find term by name and vid.
*
* @param string $name
* Term name.
* @param string $vid
* Term vid.
* @return int
* Term id, or 0 if none.
*/
public function getTidByName(string $name, string $vid):int {
if (empty($name) || empty($vid)) {
return 0;
}
$properties = [
'name' => $name,
'vid' => $vid,
];
$terms = $this->entityTypeManager->getStorage('taxonomy_term')->loadByProperties($properties);
$term = reset($terms);
return (int)(!empty($term) ? $term->id() : 0);
}
}

View File

@ -1,46 +0,0 @@
<?php
namespace Drupal\Tests\opencase\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
/**
* Simple test to ensure that main page loads with module enabled.
*
* @group opencase
*/
class LoadTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['opencase'];
/**
* A user with permission to administer site configuration.
*
* @var \Drupal\user\UserInterface
*/
protected $user;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->user = $this->drupalCreateUser(['administer site configuration']);
$this->drupalLogin($this->user);
}
/**
* Tests that the home page loads with a 200 response.
*/
public function testLoad() {
$this->drupalGet(Url::fromRoute('<front>'));
$this->assertSession()->statusCodeEquals(200);
}
}

View File

@ -0,0 +1,60 @@
<?php declare(strict_types = 1);
namespace Drupal\Tests\opencase\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\opencase\TimeBasedFieldUpdater;
class TimeBasedFieldUpdaterTest extends UnitTestCase{
function setUp():void {
/** @var \Drupal\opencase\Utils&\PHPUnit\Framework\MockObject\MockObject $utils */
$this->utils = $this->getMockBuilder('\\Drupal\\opencase\\Utils')->disableOriginalConstructor()->getMock();
/** @var \Drupal\core\Entity\EntityTypeManagerInterface&\PHPUnit\Framework\MockObject\MockObject $entityTypeManager */
$this->entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock();
$this->storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->getMock();
$this->query = $this->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryInterface')->getMock();
$this->entityTypeManager->method('getStorage')->willReturn($this->storage);
$this->storage->method('getQuery')->willReturn($this->query);
$this->updater = new TimeBasedFieldUpdater($this->entityTypeManager, $this->utils, 'dummy_entity_type', 'dummy_bundle', 'dummy_date_field');
}
function testFieldIsUpdatedOnEntityReturnedByQuery():void {
$this->query->method('execute')->willReturn([1]);
$this->entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
$this->storage->expects($this->once())->method('load')->with(1)->willReturn($this->entity);
$this->updater->update([], '3 months', ['dummy_field' => 4]);
$this->assertEquals($this->entity->dummy_field, 4);
}
function testFieldIsUpdatedOnAllEntitiesReturnedByQuery():void {
$this->query->method('execute')->willReturn([1, 2]);
$this->entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
$this->entity2 = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
$this->storage->method('load')->willReturnMap([[1, $this->entity], [2, $this-> entity2]]);
$this->updater->update([], '3 months', ['dummy_field' => 4]);
$this->assertEquals($this->entity->dummy_field, 4);
$this->assertEquals($this->entity2->dummy_field, 4);
}
function testMultipleFieldsAreUpdated(): void {
$this->query->method('execute')->willReturn([1]);
$this->entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
$this->storage->expects($this->once())->method('load')->with(1)->willReturn($this->entity);
$this->updater->update([], '3 months', ['dummy_field' => 4, 'dummy_field_2' => 5]);
$this->assertEquals($this->entity->dummy_field, 4);
$this->assertEquals($this->entity->dummy_field_2, 5);
}
function testBundleAndDateAndExtraConditionsAreAllAddedAsQueryConditions(): void {
$this->query->method('execute')->willReturn([]);
$expected_conditions= [
['dummy_field', 'dummy_value', '<'],
['dummy_field_2', 'dummy_value_2', '='],
['dummy_date_field', date('Y-m-d', strtotime('- 3 months')), "<"],
['type', 'dummy_bundle', '=']
];
$this->utils->expects($this->once())->method('addConditionsToQuery')->with($this->query, $expected_conditions);
$this->updater->update([['dummy_field', 'dummy_value', '<'], ['dummy_field_2', 'dummy_value_2', '='] ], '3 months', ['dummy_field' => 4]);
}
}

View File

@ -0,0 +1,34 @@
<?php declare(strict_types = 1);
namespace Drupal\Tests\opencase\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\opencase\Utils;
class UtilsTest extends UnitTestCase{
public function setUp(): void {
/** @var \Drupal\core\Entity\EntityTypeManager&\PHPUnit\Framework\MockObject\MockObject $entityTypeManager */
$this->entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock();
$this->utils = new Utils($this->entityTypeManager);
$this->storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->getMock();
$this->query = $this->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryInterface')->getMock();
$this->entityTypeManager->method('getStorage')->willReturn($this->storage);
}
public function testGetTidByNameGetsTid():void {
$this->entityTypeManager->method('getStorage')->willReturn($this->storage);
$term_entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
$term_entity->expects($this->once())->method('id')->willReturn('3');
$this->storage->expects($this->once())->method('loadByProperties')->with(['name' => 'foo', 'vid' => 'bar'])
->willReturn([$term_entity]);
$this->assertEquals($this->utils->getTidByName('foo', 'bar'), 3);
}
public function testAddConditionToQueryExceptsIfNotGivenArraysOf3Things():void {
$this->expectException(\RuntimeException::class);
$this->utils->addConditionsToQuery($this->query, [["1","2"], ["1"]]);
}
public function testAddConditionToQueryCallsAddsTheRightAmountOfConditions():void {
$this->query->expects($this->exactly(4))->method('condition');
$this->utils->addConditionsToQuery($this->query, [["1","2","3"], ["lk", "n", "kk"], ['sfd', 'ds', 'fds'], ["1","2","3"]]);
}
}