Chris@0: config('file.settings') Chris@0: ->set('make_unused_managed_files_temporary', TRUE) Chris@0: ->save(); Chris@0: Chris@0: $this->webUser = $this->drupalCreateUser([ Chris@0: 'access content', Chris@0: 'access comments', Chris@0: 'post comments', Chris@0: 'skip comment approval', Chris@0: ]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests creation, display, and deletion of user pictures. Chris@0: */ Chris@0: public function testCreateDeletePicture() { Chris@0: $this->drupalLogin($this->webUser); Chris@0: Chris@0: // Save a new picture. Chris@0: $image = current($this->drupalGetTestFiles('image')); Chris@0: $file = $this->saveUserPicture($image); Chris@0: Chris@0: // Verify that the image is displayed on the user account page. Chris@0: $this->drupalGet('user'); Chris@0: $this->assertRaw(file_uri_target($file->getFileUri()), 'User picture found on user account page.'); Chris@0: Chris@0: // Delete the picture. Chris@0: $edit = []; Chris@0: $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $edit, t('Remove')); Chris@0: $this->drupalPostForm(NULL, [], t('Save')); Chris@0: Chris@0: // Call file_cron() to clean up the file. Make sure the timestamp Chris@0: // of the file is older than the system.file.temporary_maximum_age Chris@0: // configuration value. Chris@0: db_update('file_managed') Chris@0: ->fields([ Chris@0: 'changed' => REQUEST_TIME - ($this->config('system.file')->get('temporary_maximum_age') + 1), Chris@0: ]) Chris@0: ->condition('fid', $file->id()) Chris@0: ->execute(); Chris@0: \Drupal::service('cron')->run(); Chris@0: Chris@0: // Verify that the image has been deleted. Chris@0: $this->assertFalse(File::load($file->id()), 'File was removed from the database.'); Chris@0: // Clear out PHP's file stat cache so we see the current value. Chris@0: clearstatcache(TRUE, $file->getFileUri()); Chris@0: $this->assertFalse(is_file($file->getFileUri()), 'File was removed from the file system.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests embedded users on node pages. Chris@0: */ Chris@0: public function testPictureOnNodeComment() { Chris@0: $this->drupalLogin($this->webUser); Chris@0: Chris@0: // Save a new picture. Chris@0: $image = current($this->drupalGetTestFiles('image')); Chris@0: $file = $this->saveUserPicture($image); Chris@0: Chris@0: $node = $this->drupalCreateNode(['type' => 'article']); Chris@0: Chris@0: // Enable user pictures on nodes. Chris@0: $this->config('system.theme.global')->set('features.node_user_picture', TRUE)->save(); Chris@0: Chris@0: $image_style_id = $this->config('core.entity_view_display.user.user.compact')->get('content.user_picture.settings.image_style'); Chris@0: $style = ImageStyle::load($image_style_id); Chris@0: $image_url = file_url_transform_relative($style->buildUrl($file->getfileUri())); Chris@0: $alt_text = 'Profile picture for user ' . $this->webUser->getUsername(); Chris@0: Chris@0: // Verify that the image is displayed on the node page. Chris@0: $this->drupalGet('node/' . $node->id()); Chris@0: $elements = $this->cssSelect('.node__meta .field--name-user-picture img[alt="' . $alt_text . '"][src="' . $image_url . '"]'); Chris@0: $this->assertEqual(count($elements), 1, 'User picture with alt text found on node page.'); Chris@0: Chris@0: // Enable user pictures on comments, instead of nodes. Chris@0: $this->config('system.theme.global') Chris@0: ->set('features.node_user_picture', FALSE) Chris@0: ->set('features.comment_user_picture', TRUE) Chris@0: ->save(); Chris@0: Chris@0: $edit = [ Chris@0: 'comment_body[0][value]' => $this->randomString(), Chris@0: ]; Chris@0: $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Save')); Chris@0: $elements = $this->cssSelect('.comment__meta .field--name-user-picture img[alt="' . $alt_text . '"][src="' . $image_url . '"]'); Chris@0: $this->assertEqual(count($elements), 1, 'User picture with alt text found on the comment.'); Chris@0: Chris@0: // Disable user pictures on comments and nodes. Chris@0: $this->config('system.theme.global') Chris@0: ->set('features.node_user_picture', FALSE) Chris@0: ->set('features.comment_user_picture', FALSE) Chris@0: ->save(); Chris@0: Chris@0: $this->drupalGet('node/' . $node->id()); Chris@0: $this->assertNoRaw(file_uri_target($file->getFileUri()), 'User picture not found on node and comment.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Edits the user picture for the test user. Chris@0: */ Chris@0: public function saveUserPicture($image) { Chris@0: $edit = ['files[user_picture_0]' => drupal_realpath($image->uri)]; Chris@0: $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $edit, t('Save')); Chris@0: Chris@0: // Load actual user data from database. Chris@0: $user_storage = $this->container->get('entity.manager')->getStorage('user'); Chris@0: $user_storage->resetCache([$this->webUser->id()]); Chris@0: $account = $user_storage->load($this->webUser->id()); Chris@0: return File::load($account->user_picture->target_id); Chris@0: } Chris@0: Chris@0: }