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