Merge branch 'master' into revisionerrors
This commit is contained in:
commit
4852de71c7
@ -31,7 +31,10 @@ final class TimeBasedFieldUpdater {
|
|||||||
$query = $this->entityTypeManager->getStorage($this->entity_type)->getQuery();
|
$query = $this->entityTypeManager->getStorage($this->entity_type)->getQuery();
|
||||||
$conditions[] = [$this->date_field, date($this->date_format, strtotime('-'.$time_elapsed)), "<"];
|
$conditions[] = [$this->date_field, date($this->date_format, strtotime('-'.$time_elapsed)), "<"];
|
||||||
$conditions[] = ['type', $this->bundle, '='];
|
$conditions[] = ['type', $this->bundle, '='];
|
||||||
$this->utils->addConditionsToQuery($query, $conditions);
|
|
||||||
|
foreach ($conditions as $condition) {
|
||||||
|
$query->condition($condition[0], $condition[1], $condition[2] ?? "=");
|
||||||
|
}
|
||||||
foreach($query->execute() as $id) {
|
foreach($query->execute() as $id) {
|
||||||
$this->updateEntity($id, $new_values);
|
$this->updateEntity($id, $new_values);
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,8 @@
|
|||||||
namespace Drupal\opencase;
|
namespace Drupal\opencase;
|
||||||
|
|
||||||
use \Drupal;
|
use \Drupal;
|
||||||
use Drupal\Core\Entity\EntityTypeManager;
|
|
||||||
use Drupal\Core\Entity\Query\QueryInterface;
|
use Drupal\Core\Entity\Query\QueryInterface;
|
||||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||||
use RuntimeException;
|
|
||||||
|
|
||||||
class Utils {
|
class Utils {
|
||||||
|
|
||||||
@ -19,15 +17,6 @@ class Utils {
|
|||||||
$this->entityTypeManager = $entityTypeManager;
|
$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.
|
* Utility: find term by name and vid.
|
||||||
*
|
*
|
||||||
@ -40,7 +29,7 @@ class Utils {
|
|||||||
*/
|
*/
|
||||||
public function getTidByName(string $name, string $vid):int {
|
public function getTidByName(string $name, string $vid):int {
|
||||||
if (empty($name) || empty($vid)) {
|
if (empty($name) || empty($vid)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
$properties = [
|
$properties = [
|
||||||
'name' => $name,
|
'name' => $name,
|
||||||
|
@ -48,13 +48,13 @@ class TimeBasedFieldUpdaterTest extends UnitTestCase{
|
|||||||
|
|
||||||
function testBundleAndDateAndExtraConditionsAreAllAddedAsQueryConditions(): void {
|
function testBundleAndDateAndExtraConditionsAreAllAddedAsQueryConditions(): void {
|
||||||
$this->query->method('execute')->willReturn([]);
|
$this->query->method('execute')->willReturn([]);
|
||||||
$expected_conditions= [
|
|
||||||
['dummy_field', 'dummy_value', '<'],
|
$this->query->expects($this->exactly(4))->method('condition')->withConsecutive(
|
||||||
['dummy_field_2', 'dummy_value_2', '='],
|
['dummy_field', 'dummy_value', '<'],
|
||||||
['dummy_date_field', date('Y-m-d', strtotime('- 3 months')), "<"],
|
['dummy_field_2', 'dummy_value_2', '='],
|
||||||
['type', 'dummy_bundle', '=']
|
['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]);
|
$this->updater->update([['dummy_field', 'dummy_value', '<'], ['dummy_field_2', 'dummy_value_2', '='] ], '3 months', ['dummy_field' => 4]);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,20 +16,10 @@ class UtilsTest extends UnitTestCase{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testGetTidByNameGetsTid():void {
|
public function testGetTidByNameGetsTid():void {
|
||||||
$this->entityTypeManager->method('getStorage')->willReturn($this->storage);
|
|
||||||
$term_entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
|
$term_entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
|
||||||
$term_entity->expects($this->once())->method('id')->willReturn('3');
|
$term_entity->expects($this->once())->method('id')->willReturn('3');
|
||||||
$this->storage->expects($this->once())->method('loadByProperties')->with(['name' => 'foo', 'vid' => 'bar'])
|
$this->storage->expects($this->once())->method('loadByProperties')->with(['name' => 'foo', 'vid' => 'bar'])
|
||||||
->willReturn([$term_entity]);
|
->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