Further modules included.
[yaffs-website] / web / modules / contrib / filefield_sources / src / Tests / ReferenceSourceTest.php
1 <?php
2
3 /**
4  * @file
5  * Definition of Drupal\filefield_sources\Tests\ReferenceSourceTest.
6  */
7
8 namespace Drupal\filefield_sources\Tests;
9
10 use Drupal\Component\Utility\Unicode;
11
12 /**
13  * Tests the reference source.
14  *
15  * @group filefield_sources
16  */
17 class ReferenceSourceTest extends FileFieldSourcesTestBase {
18
19   /**
20    * Tests reference source enabled.
21    */
22   public function testReferenceSourceEnabled() {
23
24     // Create test file.
25     $file = $this->createPermanentFileEntity();
26
27     $this->enableSources(array(
28       'reference' => TRUE,
29     ));
30
31     // Upload a file by 'Reference' source.
32     $this->uploadFileByReferenceSource($file->id(), $file->getFilename(), 0);
33
34     // We can only refer one file on single value field.
35     $this->assertNoFieldByXPath('//input[@type="submit"]', t('Select'), t('After uploading a file, "Select" button is no longer displayed.'));
36
37     // Remove uploaded file.
38     $this->removeFile($file->getFileName(), 0);
39
40     // Can select file again.
41     $this->assertFieldByXpath('//input[@type="submit"]', t('Select'), 'After clicking the "Remove" button, the "Select" button is displayed.');
42   }
43
44   /**
45    * Test autocompletion.
46    */
47   public function testAutocompletion() {
48     // Create test file.
49     $file = $this->createPermanentFileEntity();
50     $filename = $file->getFileName();
51     $first_character = substr($filename, 0, 1);
52     $second_character = substr($filename, 1, 1);
53
54     // Switch to 'Starts with' match type.
55     $this->updateFilefieldSourcesSettings('source_reference', 'autocomplete', '0');
56
57     // STARTS_WITH: empty results.
58     $query = $this->findCharacterNotInString($first_character);
59     $autocomplete_result = $this->drupalGetJSON('file/reference/node/' . $this->typeName . '/' . $this->fieldName, array('query' => array('q' => $query)));
60     $this->assertEqual($autocomplete_result, array(), "No files that have name starts with '$query'");
61
62     // STARTS_WITH: not empty results.
63     $query = $first_character;
64     $autocomplete_result = $this->drupalGetJSON('file/reference/node/' . $this->typeName . '/' . $this->fieldName, array('query' => array('q' => $query)));
65     $this->assertEqual($autocomplete_result[0]['label'], $filename, 'Autocompletion return correct label.');
66     $this->assertEqual($autocomplete_result[0]['value'], $filename . ' [fid:' . $file->id() . ']', 'Autocompletion return correct value.');
67
68     // Switch to 'Contains' match type.
69     $this->updateFilefieldSourcesSettings('source_reference', 'autocomplete', '1');
70
71     // CONTAINS: empty results.
72     $query = $this->findCharacterNotInString($filename);
73     $autocomplete_result = $this->drupalGetJSON('file/reference/node/' . $this->typeName . '/' . $this->fieldName, array('query' => array('q' => $query)));
74     $this->assertEqual($autocomplete_result, array(), "No files that have name contains '$query'");
75
76     // CONTAINS: not empty results.
77     $query = $second_character;
78     $autocomplete_result = $this->drupalGetJSON('file/reference/node/' . $this->typeName . '/' . $this->fieldName, array('query' => array('q' => $query)));
79     $this->assertEqual($autocomplete_result[0]['label'], $filename, 'Autocompletion return correct label.');
80     $this->assertEqual($autocomplete_result[0]['value'], $filename . ' [fid:' . $file->id() . ']', 'Autocompletion return correct value.');
81   }
82
83   /**
84    * Find the first character that is not in string.
85    *
86    * Only find for lower case character.
87    *
88    * @param string $string
89    *   String to check.
90    *
91    * @return string
92    *   First character that is not in the string.
93    */
94   protected function findCharacterNotInString($string) {
95     // Only check for lower case string.
96     $string = Unicode::strtolower($string);
97
98     // Lower case characters and numbers generated by
99     // \Drupal\simpletest\TestBase::randomMachineName().
100     $values = array_merge(range(97, 122), range(48, 57));
101     foreach ($values as $value) {
102       $character = chr($value);
103       if (strpos($string, $character) === FALSE) {
104         return $character;
105       }
106     }
107   }
108
109 }