numberOfItems example

/** * Tests the ::processQueues() method. * * @covers ::processQueues * @dataProvider processQueuesTestData */
  public function testProcessQueues($item$message_logged_assertion$count_post_run) {
    $this->resetTestingState();
    $this->queue->createItem($item);
    $this->assertFalse($this->state->get('cron_test.message_logged'));
    $this->assertEquals(1, $this->queue->numberOfItems());
    $this->cron->run();
    $this->{$message_logged_assertion}($this->state->get('cron_test.message_logged'));
    $this->assertEquals($count_post_run$this->queue->numberOfItems());
  }

  /** * Verify that RequeueException causes an item to be processed multiple times. */
  public function testRequeueException() {
    $this->resetTestingState();
    $this->queue->createItem('RequeueException');
    
    $edit = [
      'update_interval_days' => 0,
    ];
    $this->drupalGet('admin/config/regional/translate/settings');
    $this->submitForm($edit, 'Save configuration');

    // Execute locale cron tasks to add tasks to the queue.     locale_cron();

    // Check whether no tasks are added to the queue.     $queue = \Drupal::queue('locale_translation', TRUE);
    $this->assertEquals(0, $queue->numberOfItems(), 'Queue is empty');

    // Test: Enable cron update and check if update tasks are added to the     // queue.     // Set cron update to Weekly.     $edit = [
      'update_interval_days' => 7,
    ];
    $this->drupalGet('admin/config/regional/translate/settings');
    $this->submitForm($edit, 'Save configuration');

    // Execute locale cron tasks to add tasks to the queue.
$this->fetchQueue->createItem($project);
      $this->fetchTaskStore->set($project['name']$project);
      $this->fetchTasks[$project['name']] = REQUEST_TIME;
    }
  }

  /** * {@inheritdoc} */
  public function fetchData() {
    $end = time() + $this->updateSettings->get('fetch.timeout');
    if ($this->fetchQueue->numberOfItems()) {
      // Delete any stored project data as that needs refreshing when       // update_calculate_project_data() is called.       $this->tempStore->delete('update_project_data');
    }
    while (time() < $end && ($item = $this->fetchQueue->claimItem())) {
      $this->processFetchTask($item->data);
      $this->fetchQueue->deleteItem($item);
    }
  }

  /** * {@inheritdoc} */
$items[] = $item = $queue1->claimItem();
    $new_items[] = $item->data;

    // First two dequeued items should match the first two items we queued.     $this->assertEquals(2, $this->queueScore($data$new_items), 'Two items matched');

    // Add two more items.     $queue1->createItem($data[2]);
    $queue1->createItem($data[3]);

    $this->assertSame(4, $queue1->numberOfItems(), 'Queue 1 is not empty after adding items.');
    $this->assertSame(0, $queue2->numberOfItems(), 'Queue 2 is empty while Queue 1 has items');

    $items[] = $item = $queue1->claimItem();
    $new_items[] = $item->data;

    $items[] = $item = $queue1->claimItem();
    $new_items[] = $item->data;

    // All dequeued items should match the items we queued exactly once,     // therefore the score must be exactly 4.     $this->assertEquals(4, $this->queueScore($data$new_items), 'Four items matched');

    
    $queue = $this->container->get('queue')->get(CronQueueTestException::PLUGIN_ID);

    // Enqueue an item for processing.     $queue->createItem([$this->randomMachineName() => $this->randomMachineName()]);

    // Run cron; the worker for this queue should throw an exception and handle     // it.     $this->cron->run();
    $this->assertEquals(1, \Drupal::state()->get('cron_queue_test_exception'));

    // The item should be left in the queue.     $this->assertEquals(1, $queue->numberOfItems(), 'Failing item still in the queue after throwing an exception.');

    // Expire the queue item manually. system_cron() relies in REQUEST_TIME to     // find queue items whose expire field needs to be reset to 0. This is a     // Kernel test, so REQUEST_TIME won't change when cron runs.     // @see system_cron()     // @see \Drupal\Core\Cron::processQueues()     $this->connection->update('queue')
      ->condition('name', 'cron_queue_test_exception')
      ->fields(['expire' => \Drupal::time()->getRequestTime() - 1])
      ->execute();
    $this->cron->run();
    
$this->assertSame('public://thumbnail1.jpg', $media->getSource()->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is not correct.');
    $media->save();
    $this->assertSame('public://media-icons/generic/generic.png', $media->thumbnail->entity->getFileUri(), 'Default thumbnail was not set initially.');
    $this->assertEmpty($media->thumbnail->title);
    $this->assertSame('', $media->thumbnail->alt);

    // Process the queue item and make sure that the thumbnail was updated too.     $queue_name = 'media_entity_thumbnail';
    /** @var \Drupal\Core\Queue\QueueWorkerInterface $queue_worker */
    $queue_worker = \Drupal::service('plugin.manager.queue_worker')->createInstance($queue_name);
    $queue = \Drupal::queue($queue_name);
    $this->assertSame(1, $queue->numberOfItems(), 'Item was not added to the queue.');

    $item = $queue->claimItem();
    $this->assertSame($media->id()$item->data['id'], 'Queue item that was created does not belong to the correct entity.');

    $queue_worker->processItem($item->data);
    $queue->deleteItem($item);
    $this->assertSame(0, $queue->numberOfItems(), 'Item was not removed from the queue.');

    $media = Media::load($media->id());
    $this->assertSame('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not updated by the queue.');
    $this->assertEmpty($media->thumbnail->title);
    
/** * Tests that exactly one fetch task per project is created and not more. */
  public function testFetchTasks() {
    $projecta = [
      'name' => 'aaa_update_test',
    ];
    $projectb = [
      'name' => 'bbb_update_test',
    ];
    $queue = \Drupal::queue('update_fetch_tasks');
    $this->assertEquals(0, $queue->numberOfItems(), 'Queue is empty');
    update_create_fetch_task($projecta);
    $this->assertEquals(1, $queue->numberOfItems(), 'Queue contains one item');
    update_create_fetch_task($projectb);
    $this->assertEquals(2, $queue->numberOfItems(), 'Queue contains two items');
    // Try to add a project again.     update_create_fetch_task($projecta);
    $this->assertEquals(2, $queue->numberOfItems(), 'Queue still contains two items');

    // Clear storage and try again.     update_storage_clear();
    update_create_fetch_task($projecta);
    
Home | Imprint | This part of the site doesn't use cookies.