setOwner example

// Unpublish the top book page and confirm that the created book title is     // displayed for user which has 'view own unpublished content' permission.     $this->drupalLogin($this->bookAuthor);
    $this->book->setUnpublished();
    $this->book->save();

    $this->drupalGet('book');
    $this->assertSession()->pageTextContains($this->book->label());

    // Ensure the user doesn't see the book if they don't own it.     $this->book->setOwner($this->webUser)->save();
    $this->drupalGet('book');
    $this->assertSession()->pageTextNotContains($this->book->label());

    // Confirm that the created book title is displayed for user which has     // 'view any unpublished content' permission.     $this->drupalLogin($this->adminUser);
    $this->drupalGet('book');
    $this->assertSession()->pageTextContains($this->book->label());
  }

  /** * Tests the administrative listing of all books. */

  public function testUnsavedNodeOwner() {
    $user = User::create([
      'name' => 'foo',
    ]);
    $node = Node::create([
      'type' => 'page',
      'title' => $this->randomMachineName(),
    ]);
    // Set the node owner while the user is unsaved and then immediately save     // the user and node.     $node->setOwner($user);
    $user->save();
    $node->save();

    // The ID assigned to the newly saved user will now be the owner ID of the     // node.     $this->assertEquals($user->id()$node->getOwnerId());
  }

}
$user = $this->drupalCreateUser([
      'access content',
      'edit own journal_article content',
    ]);
    $article_node = Node::create([
      'title' => 'Test Journal Article',
      'type' => 'journal_article',
      'field_issue' => [
        'target_id' => $issue_node->id(),
      ],
    ]);
    $article_node->setOwner($user);
    $article_node->save();

    // Test.     $url = Url::fromUri(sprintf('internal:/jsonapi/node/journal_article/%s', $article_node->uuid()));
    $request_options = [
      RequestOptions::HEADERS => [
        'Content-Type' => 'application/vnd.api+json',
        'Accept' => 'application/vnd.api+json',
      ],
      RequestOptions::AUTH => [$user->getAccountName()$user->pass_raw],
      RequestOptions::JSON => [
        
public function testArgument() {
    $expected_result = [];

    $author = $this->createUser();
    $no_author = $this->createUser();

    // Create one node, with the author as the node author.     $node1 = Node::create([
      'type' => 'default',
      'title' => $this->randomMachineName(),
    ]);
    $node1->setOwner($author);
    $node1->save();
    $expected_result[] = ['nid' => $node1->id()];

    // Create one node of which an additional revision author will be the     // author.     $node2 = Node::create([
      'type' => 'default',
      'title' => $this->randomMachineName(),
    ]);
    $node2->setRevisionUserId($no_author->id());
    $node2->save();
    
$this->grantPermissionsToTestedRole(['delete any file']);
        break;
    }
  }

  /** * Makes the current user the file owner. */
  protected function makeCurrentUserFileOwner() {
    $account = User::load(2);
    $this->entity->setOwnerId($account->id());
    $this->entity->setOwner($account);
    $this->entity->save();
  }

  /** * {@inheritdoc} */
  protected function createEntity() {
    $this->author = User::load(1);

    $file = File::create();
    $file->setOwnerId($this->author->id());
    
$this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);

    // Create user.     $this->searchingUser = $this->drupalCreateUser([
      'search content',
      'access user profiles',
    ]);

    // Create a node and update the search index.     $this->node = $this->drupalCreateNode(['title' => 'bike shed shop']);
    $this->node->setOwner($this->searchingUser);
    $this->node->save();
    $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
  }

  /** * Tests the presence of the expected cache tag in various situations. */
  public function testSearchText() {
    $this->drupalLogin($this->searchingUser);

    // Initial page for searching nodes.
'name' => $this->randomString(),
      'user_id' => $user1->id(),
      'language' => 'en',
      'non_rev_field' => 'Michigan',
    ]);
    $entity2->save();

    $this->assertEquals('Huron', $entity->get('non_rev_field')->value, 'Huron found on entity 1');
    $this->assertEquals('Michigan', $entity2->get('non_rev_field')->value, 'Michigan found on entity 2');

    $entity->setNewRevision();
    $entity->setOwner($user2);
    $entity->save();
    $entity2->setNewRevision();
    $entity2->setOwner($user2);
    $entity2->save();
    $this->assertEquals($user2->id()$entity->getOwner()->id(), 'User 2 found on entity 1');
    $this->assertEquals($user2->id()$entity2->getOwner()->id(), 'User 2 found on entity 2');

    $entity->addTranslation('de');
    $entity->save();
    $entity2->addTranslation('de');
    $entity2->save();

    

  protected function createTemporaryFile($data, UserInterface $user = NULL) {
    /** @var \Drupal\file\FileRepositoryInterface $file_repository */
    $file_repository = \Drupal::service('file.repository');
    $file = $file_repository->writeData($data, "public://");

    if ($file) {
      if ($user) {
        $file->setOwner($user);
      }
      else {
        $file->setOwner($this->adminUser);
      }
      // Change the file status to be temporary.       $file->setTemporary();
      // Save the changes.       $file->save();
    }

    return $file;
  }
$this->assertTrue(
      $german->getChangedTime() > $changed_de,
      'Changed time of the German translation did change.'
    );

    $this->assertEquals($entity->getChangedTime()$german->getChangedTime(), 'When editing a non-translatable field the updated changed time is equal across all translations.');

    $changed_en = $entity->getChangedTime();
    $changed_de = $german->getChangedTime();

    $entity->setOwner($user2);

    $entity->save();

    $this->assertTrue(
      $entity->getChangedTime() > $changed_en,
      'Changed time of original language did change.'
    );

    $this->assertEquals($changed_de$german->getChangedTime(), 'Changed time of the German translation did not change.');

    $this->assertTrue(
      
->setTranslatable(FALSE)
      ->save();
    $this->drupalCreateContentType(['type' => 'x']);
    $this->rebuildAll();
    $this->grantPermissionsToTestedRole(['access content']);

    // Create data.     $user_a = User::create([])->setUsername('A')->activate();
    $user_a->save();
    $user_b = User::create([])->setUsername('B')->set('field_favorite_animal', 'stegosaurus')->block();
    $user_b->save();
    $node_a = Node::create(['type' => 'x'])->setTitle('Owned by A')->setOwner($user_a);
    $node_a->save();
    $node_b = Node::create(['type' => 'x'])->setTitle('Owned by B')->setOwner($user_b);
    $node_b->save();
    $node_anon_1 = Node::create(['type' => 'x'])->setTitle('Owned by anon #1')->setOwnerId(0);
    $node_anon_1->save();
    $node_anon_2 = Node::create(['type' => 'x'])->setTitle('Owned by anon #2')->setOwnerId(0);
    $node_anon_2->save();
    $node_auth_1 = Node::create(['type' => 'x'])->setTitle('Owned by auth #1')->setOwner($this->account);
    $node_auth_1->save();

    $favorite_animal_test_url = Url::fromRoute('jsonapi.user--user.collection')->setOption('query', ['filter[field_favorite_animal]' => 'stegosaurus']);

    
$this->assertSame($expected$changed_element1->getText());

    // Operations.     $assert_session->elementExists('css', 'td.views-field-operations li a:contains("Edit")', $row1);
    $assert_session->linkByHrefExists('/media/' . $media1->id() . '/edit');
    $assert_session->elementExists('css', 'td.views-field-operations li a:contains("Delete")', $row1);
    $assert_session->linkByHrefExists('/media/' . $media1->id() . '/delete');

    // Make the user the owner of the unpublished media item and assert the     // media item is only visible with the 'view own unpublished media'     // permission.     $media2->setOwner($this->nonAdminUser)->save();
    $this->getSession()->reload();
    $assert_session->pageTextNotContains($media2->label());
    $role->grantPermission('view own unpublished media')->save();
    $this->getSession()->reload();
    $row = $assert_session->elementExists('css', 'table tbody tr:nth-child(2)');
    $name = $assert_session->elementExists('css', 'td.views-field-name a', $row);
    $this->assertSame($media2->label()$name->getText());
    $status_element = $assert_session->elementExists('css', 'td.views-field-status', $row);
    $this->assertSame('Unpublished', $status_element->getText());

    // Assert the admin user can always view all media.
$this->grantPermissionsToTestedRole(['delete any file']);
        break;
    }
  }

  /** * Makes the current user the file owner. */
  protected function makeCurrentUserFileOwner() {
    $account = static::$auth ? User::load(2) : User::load(0);
    $this->entity->setOwnerId($account->id());
    $this->entity->setOwner($account);
    $this->entity->save();
  }

  /** * {@inheritdoc} */
  protected function createEntity() {
    $this->author = User::load(1);

    $file = File::create();
    $file->setOwnerId($this->author->id());
    
// Create a content type with a body field.     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);

    // Create a file in the 'private:// ' stream.     $filename = 'test.png';
    $src = '/system/files/' . $filename;
    /** @var \Drupal\file\FileInterface $file */
    $file = File::create([
      'uri' => 'private://' . $filename,
    ]);
    $file->setTemporary();
    $file->setOwner($author);
    // Create the file itself.     file_put_contents($file->getFileUri()$this->randomString());
    $file->save();

    // The image should be visible for its author.     $this->drupalGet($src);
    $this->assertSession()->statusCodeEquals(200);
    // The not-yet-permanent image should NOT be visible for anonymous.     $this->drupalLogout();
    $this->drupalGet($src);
    $this->assertSession()->statusCodeEquals(403);

    
$file_storage = $this->entityTypeManager()->getStorage('file');

    $existing = $file_storage->loadByProperties($values);
    if ($existing) {
      $file = reset($existing);
    }
    else {
      /** @var \Drupal\file\FileInterface $file */
      $file = $file_storage->create($values);
      if ($owner = $this->getOwner()) {
        $file->setOwner($owner);
      }
      $file->setPermanent();
      $file->save();
    }
    return $file;
  }

  /** * Returns the URI of the default thumbnail. * * @return string * The default thumbnail URI. */
$media = Media::create([
      'uuid' => static::EMBEDDED_ENTITY_UUID,
      'bundle' => 'image',
      'name' => 'Screaming hairy armadillo',
      'field_media_image' => [
        [
          'target_id' => $this->image->id(),
          'alt' => 'default alt',
          'title' => 'default title',
        ],
      ],
    ])->setOwner($user);
    $media->save();
    $this->embeddedEntity = $media;
  }

  /** * Gets an embed code with given attributes. * * @param array $attributes * The attributes to add. * * @return string * A string containing a drupal-media DOM element. * * @see assertEntityEmbedFilterHasRun() */
Home | Imprint | This part of the site doesn't use cookies.