/** * Tests normal formatter display on node display. */ function testNodeDisplay() { $field_name = strtolower($this->randomMachineName()); $type_name = 'article'; $field_storage_settings = array('display_field' => '1', 'display_default' => '1', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); $field_settings = array('description_field' => '1'); $widget_settings = array(); $this->createFileField($field_name, 'node', $type_name, $field_storage_settings, $field_settings, $widget_settings); // Create a new node *without* the file field set, and check that the field // is not shown for each node display. $node = $this->drupalCreateNode(array('type' => $type_name)); // Check file_default last as the assertions below assume that this is the // case. $file_formatters = array('file_table', 'file_url_plain', 'hidden', 'file_default'); foreach ($file_formatters as $formatter) { $edit = array("fields[{$field_name}][type]" => $formatter); $this->drupalPostForm("admin/structure/types/manage/{$type_name}/display", $edit, t('Save')); $this->drupalGet('node/' . $node->id()); $this->assertNoText($field_name, format_string('Field label is hidden when no file attached for formatter %formatter', array('%formatter' => $formatter))); } $test_file = $this->getTestFile('text'); simpletest_generate_file('escaped-&-text', 64, 10, 'text'); $test_file = File::create(['uri' => 'public://escaped-&-text.txt', 'name' => 'escaped-&-text', 'filesize' => filesize('public://escaped-&-text.txt')]); // Create a new node with the uploaded file. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name); // Check that the default formatter is displaying with the file name. $node_storage = $this->container->get('entity.manager')->getStorage('node'); $node_storage->resetCache(array($nid)); $node = $node_storage->load($nid); $node_file = File::load($node->{$field_name}->target_id); $file_link = array('#theme' => 'file_link', '#file' => $node_file); $default_output = \Drupal::service('renderer')->renderRoot($file_link); $this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.'); // Turn the "display" option off and check that the file is no longer displayed. $edit = array($field_name . '[0][display]' => FALSE); $this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save and keep published')); $this->assertNoRaw($default_output, 'Field is hidden when "display" option is unchecked.'); // Add a description and make sure that it is displayed. $description = $this->randomMachineName(); $edit = array($field_name . '[0][description]' => $description, $field_name . '[0][display]' => TRUE); $this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save and keep published')); $this->assertText($description); // Ensure the filename in the link's title attribute is escaped. $this->assertRaw('title="escaped-&-text.txt"'); // Test that fields appear as expected after during the preview. // Add a second file. $name = 'files[' . $field_name . '_1][]'; $edit[$name] = drupal_realpath($test_file->getFileUri()); // Uncheck the display checkboxes and go to the preview. $edit[$field_name . '[0][display]'] = FALSE; $edit[$field_name . '[1][display]'] = FALSE; $this->drupalPostForm("node/{$nid}/edit", $edit, t('Preview')); $this->clickLink(t('Back to content editing')); $this->assertRaw($field_name . '[0][display]', 'First file appears as expected.'); $this->assertRaw($field_name . '[1][display]', 'Second file appears as expected.'); }
/** * Gets a list of files that can be used in tests. * * The first time this method is called, it will call * simpletest_generate_file() to generate binary and ASCII text files in the * public:// directory. It will also copy all files in * core/modules/simpletest/files to public://. These contain image, SQL, PHP, * JavaScript, and HTML files. * * All filenames are prefixed with their type and have appropriate extensions: * - text-*.txt * - binary-*.txt * - html-*.html and html-*.txt * - image-*.png, image-*.jpg, and image-*.gif * - javascript-*.txt and javascript-*.script * - php-*.txt and php-*.php * - sql-*.txt and sql-*.sql * * Any subsequent calls will not generate any new files, or copy the files * over again. However, if a test class adds a new file to public:// that * is prefixed with one of the above types, it will get returned as well, even * on subsequent calls. * * @param $type * File type, possible values: 'binary', 'html', 'image', 'javascript', * 'php', 'sql', 'text'. * @param $size * (optional) File size in bytes to match. Defaults to NULL, which will not * filter the returned list by size. * * @return * List of files in public:// that match the filter(s). */ protected function drupalGetTestFiles($type, $size = NULL) { if (empty($this->generatedTestFiles)) { // Generate binary test files. $lines = array(64, 1024); $count = 0; foreach ($lines as $line) { simpletest_generate_file('binary-' . $count++, 64, $line, 'binary'); } // Generate ASCII text test files. $lines = array(16, 256, 1024, 2048, 20480); $count = 0; foreach ($lines as $line) { simpletest_generate_file('text-' . $count++, 64, $line, 'text'); } // Copy other test files from simpletest. $original = drupal_get_path('module', 'simpletest') . '/files'; $files = file_scan_directory($original, '/(html|image|javascript|php|sql)-.*/'); foreach ($files as $file) { file_unmanaged_copy($file->uri, PublicStream::basePath()); } $this->generatedTestFiles = TRUE; } $files = array(); // Make sure type is valid. if (in_array($type, array('binary', 'html', 'image', 'javascript', 'php', 'sql', 'text'))) { $files = file_scan_directory('public://', '/' . $type . '\\-.*/'); // If size is set then remove any files that are not of that size. if ($size !== NULL) { foreach ($files as $file) { $stats = stat($file->uri); if ($stats['size'] != $size) { unset($files[$file->uri]); } } } } usort($files, array($this, 'drupalCompareFiles')); return $files; }