fixed the unit test
This commit is contained in:
parent
a4aa8444e3
commit
fdf00d5c95
@ -14,11 +14,11 @@ final class TimeBasedFieldUpdater {
|
||||
private string $bundle;
|
||||
|
||||
final public function __construct(
|
||||
EntityTypeManagerInterface $entityTypeManager,
|
||||
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;
|
||||
@ -31,18 +31,19 @@ final class TimeBasedFieldUpdater {
|
||||
$query = $this->entityTypeManager->getStorage($this->entity_type)->getQuery();
|
||||
$conditions[] = [$this->date_field, date($this->date_format, strtotime('-'.$time_elapsed)), "<"];
|
||||
$conditions[] = ['type', $this->bundle, '='];
|
||||
|
||||
foreach ($conditions as $condition) {
|
||||
$query->condition($condition);
|
||||
$query->condition($condition[0], $condition[1], $condition[2] ?? "=");
|
||||
}
|
||||
foreach($query->execute() as $id) {
|
||||
$this->updateEntity($id, $new_values);
|
||||
}
|
||||
}
|
||||
}
|
||||
private function updateEntity(int $entity_id, array $new_values): void {
|
||||
$entity = $this->entityTypeManager->getStorage($this->entity_type)->load($entity_id);
|
||||
foreach($new_values as $new_field=>$new_value) {
|
||||
$entity->$new_field = $new_value;
|
||||
$entity->$new_field = $new_value;
|
||||
}
|
||||
$entity->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,15 +17,6 @@ class Utils {
|
||||
$this->entityTypeManager = $entityTypeManager;
|
||||
}
|
||||
|
||||
public function addConditionsToQuery(QueryInterface $query, array $conditions): void {
|
||||
foreach($conditions as $condition) {
|
||||
$field = $condition[0];
|
||||
$value = $condition[1];
|
||||
$operator = isset($condition[2]) ? $condition[2] : "=";
|
||||
$query->condition($field, $value, $operator);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility: find term by name and vid.
|
||||
*
|
||||
|
@ -22,7 +22,7 @@ class TimeBasedFieldUpdaterTest extends UnitTestCase{
|
||||
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->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);
|
||||
}
|
||||
@ -30,16 +30,16 @@ class TimeBasedFieldUpdaterTest extends UnitTestCase{
|
||||
$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->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);
|
||||
$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->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);
|
||||
@ -48,13 +48,13 @@ class TimeBasedFieldUpdaterTest extends UnitTestCase{
|
||||
|
||||
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->query->expects($this->exactly(4))->method('condition')->withConsecutive(
|
||||
['dummy_field', 'dummy_value', '<'],
|
||||
['dummy_field_2', 'dummy_value_2', '='],
|
||||
['dummy_date_field', date('Y-m-d', strtotime('- 3 months')), "<"],
|
||||
['type', 'dummy_bundle', '=']);
|
||||
|
||||
$this->updater->update([['dummy_field', 'dummy_value', '<'], ['dummy_field_2', 'dummy_value_2', '='] ], '3 months', ['dummy_field' => 4]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ class UtilsTest extends UnitTestCase{
|
||||
$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);
|
||||
$this->entityTypeManager->method('getStorage')->willReturn($this->storage);
|
||||
}
|
||||
|
||||
public function testGetTidByNameGetsTid():void {
|
||||
@ -20,15 +20,6 @@ class UtilsTest extends UnitTestCase{
|
||||
$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);
|
||||
$this->assertEquals($this->utils->getTidByName('foo', 'bar'), 3);
|
||||
}
|
||||
public function testAddConditionToQueryAddsEqualsIfNoOperatorProvided():void {
|
||||
$this->query->expects($this->exactly(1))->method('condition')->with("1", "2", "=");
|
||||
$this->utils->addConditionsToQuery($this->query, [["1","2"]]);
|
||||
|
||||
}
|
||||
public function testAddConditionToQueryAddsTheRightAmountOfConditions():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"]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user