fixed the unit test
This commit is contained in:
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user