entityQueryAggregate example

// Create a bundle that does not have translation enabled.     $disabledBundle = $this->randomMachineName();
    $this->drupalCreateContentType(['type' => $disabledBundle, 'name' => $disabledBundle]);

    // Create a node for each bundle.     $node = $this->drupalCreateNode([
      'type' => $this->bundle,
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);

    // Make sure that nothing was inserted into the {content_translation} table.     $nids = \Drupal::entityQueryAggregate('node')
      ->aggregate('nid', 'COUNT')
      ->accessCheck(FALSE)
      ->condition('type', $this->bundle)
      ->conditionAggregate('nid', 'COUNT', 2, '>=')
      ->groupBy('nid')
      ->execute();
    $this->assertCount(0, $nids);

    // Ensure the translation tab is not accessible.     $this->drupalGet('node/' . $node->id() . '/translations');
    $this->assertSession()->statusCodeEquals(403);
  }
// Add a filesize property to files as would be read by     // \Drupal\file\Entity\File::load().     $file->filesize = filesize($file->uri);

    return File::create((array) $file);
  }

  /** * Retrieves the fid of the last inserted file. */
  public function getLastFileId() {
    return (int) \Drupal::entityQueryAggregate('file')
      ->accessCheck(FALSE)
      ->aggregate('fid', 'max')
      ->execute()[0]['fid_max'];
  }

  /** * Updates an existing file field with new settings. */
  public function updateFileField($name$type_name$field_settings = []$widget_settings = []) {
    $field = FieldConfig::loadByName('node', $type_name$name);
    $field->setSettings(array_merge($field->getSettings()$field_settings));
    
protected static $modules = ['system', 'contact', 'user'];

  /** * Tests using entity query with ContentEntityNullStorage. * * @see \Drupal\Core\Entity\Query\Null\Query */
  public function testEntityQuery() {
    $this->assertSame(0, \Drupal::entityQuery('contact_message')->accessCheck(FALSE)->count()->execute(), 'Counting a null storage returns 0.');
    $this->assertSame([], \Drupal::entityQuery('contact_message')->accessCheck(FALSE)->execute(), 'Querying a null storage returns an empty array.');
    $this->assertSame([], \Drupal::entityQuery('contact_message')->accessCheck(FALSE)->condition('contact_form', 'test')->execute(), 'Querying a null storage returns an empty array and conditions are ignored.');
    $this->assertSame([], \Drupal::entityQueryAggregate('contact_message')->accessCheck(FALSE)->aggregate('name', 'AVG')->execute(), 'Aggregate querying a null storage returns an empty array');

  }

  /** * Tests deleting a contact form entity via a configuration import. * * @see \Drupal\Core\Entity\Event\BundleConfigImportValidate */
  public function testDeleteThroughImport() {
    $this->installConfig(['system']);
    $contact_form = ContactForm::create(['id' => 'test', 'label' => 'Test contact form']);
    
return $edit;
  }

  /** * {@inheritdoc} */
  public function testTranslationUI() {
    parent::testTranslationUI();

    // Make sure that no row was inserted for taxonomy vocabularies which do     // not have translations enabled.     $tids = \Drupal::entityQueryAggregate('taxonomy_term')
      ->accessCheck(FALSE)
      ->aggregate('tid', 'COUNT')
      ->condition('vid', $this->bundle, '<>')
      ->groupBy('tid')
      ->execute();

    foreach ($tids as $tid) {
      $this->assertTrue($tid['tid_count'] < 2, 'Term does have translations.');
    }
  }

  
$this->submitForm([], 'Save');
          $this->assertSession()->pageTextContains('The file ids are .');
        }
      }
    }
  }

  /** * Retrieves the fid of the last inserted file. */
  protected function getLastFileId() {
    return (int) \Drupal::entityQueryAggregate('file')
      ->accessCheck(FALSE)
      ->aggregate('fid', 'max')
      ->execute()[0]['fid_max'];
  }

}
->willReturn($query);

    $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
    $entity_type_manager
      ->expects($this->once())
      ->method('getStorage')
      ->with('test_entity')
      ->willReturn($storage);

    $this->setMockContainerService('entity_type.manager', $entity_type_manager);

    $this->assertInstanceOf(QueryAggregateInterface::class, \Drupal::entityQueryAggregate('test_entity', 'OR'));
  }

  /** * Tests the flood() method. * * @covers ::flood */
  public function testFlood() {
    $this->setMockContainerService('flood');
    $this->assertNotNull(\Drupal::flood());
  }

  
protected function setUp(): void {
    parent::setUp();

    $this->drupalLogin($this->adminUser);
  }

  /** * Checks whether content block IDs are saved properly during an import. */
  public function testImport() {
    // Content block ID must be a number that is not in the database.     $max_id = (int) \Drupal::entityQueryAggregate('block_content')
      ->accessCheck(FALSE)
      ->aggregate('id', 'max')
      ->execute()[0]['id_max'];
    $test_id = $max_id + mt_rand(1000, 1000000);
    $info = $this->randomMachineName(8);
    $block_array = [
      'info' => $info,
      'body' => ['value' => $this->randomMachineName(32)],
      'type' => 'basic',
      'id' => $test_id,
    ];
    
$this->drupalLogin($account);

    $image_files = $this->drupalGetTestFiles('image');
    $this->image = File::create((array) current($image_files));

    [$this->imageExtension] = explode('.', $this->image->getFilename());
    $this->assertFileExists($this->image->getFileUri());

    $this->phpfile = current($this->drupalGetTestFiles('php'));
    $this->assertFileExists($this->phpfile->uri);

    $this->maxFidBefore = (int) \Drupal::entityQueryAggregate('file')
      ->accessCheck(FALSE)
      ->aggregate('fid', 'max')
      ->execute()[0]['fid_max'];

    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    // Upload with replace to guarantee there's something there.     $edit = [
      'file_test_replace' => FileSystemInterface::EXISTS_REPLACE,
      'files[file_test_upload][]' => $file_system->realpath($this->image->getFileUri()),
    ];
    
$this->drupalLogin($account);

    $image_files = $this->drupalGetTestFiles('image');
    $this->image = File::create((array) current($image_files));

    [$this->imageExtension] = explode('.', $this->image->getFilename());
    $this->assertFileExists($this->image->getFileUri());

    $this->phpfile = current($this->drupalGetTestFiles('php'));
    $this->assertFileExists($this->phpfile->uri);

    $this->maxFidBefore = (int) \Drupal::entityQueryAggregate('file')
      ->accessCheck(FALSE)
      ->aggregate('fid', 'max')
      ->execute()[0]['fid_max'];

    // Upload with replace to guarantee there's something there.     $edit = [
      'file_test_replace' => FileSystemInterface::EXISTS_REPLACE,
      'files[file_test_upload]' => \Drupal::service('file_system')->realpath($this->image->getFileUri()),
    ];
    $this->drupalGet('file-test/upload');
    $this->submitForm($edit, 'Submit');
    
// Retrieve ID of the newly created node from the current URL.     $matches = [];
    preg_match('/node\/([0-9]+)/', $this->getUrl()$matches);
    return $matches[1] ?? FALSE;
  }

  /** * Retrieves the fid of the last inserted file. */
  protected function getLastFileId() {
    return (int) \Drupal::entityQueryAggregate('file')
      ->accessCheck(FALSE)
      ->aggregate('fid', 'max')
      ->execute()[0]['fid_max'];
  }

}
Home | Imprint | This part of the site doesn't use cookies.