HandlerTest.php 13.6 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Tests\Handler\HandlerTest.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Drupal\views\Tests\Handler;
9

10
use Drupal\views\ViewExecutable;
11
use Drupal\views\Tests\ViewTestBase;
12
use Drupal\views\Plugin\views\HandlerBase;
tim.plunkett's avatar
tim.plunkett committed
13

merlinofchaos's avatar
merlinofchaos committed
14 15 16
/**
 * Tests abstract handlers of views.
 */
17
class HandlerTest extends ViewTestBase {
18

19 20 21 22 23
  /**
   * Modules to enable.
   *
   * @var array
   */
24
  public static $modules = array('views_ui', 'comment', 'node');
25

merlinofchaos's avatar
merlinofchaos committed
26 27
  public static function getInfo() {
    return array(
28
      'name' => 'Handler: Base',
29
      'description' => 'Tests abstract handler definitions.',
30
      'group' => 'Views Handlers',
merlinofchaos's avatar
merlinofchaos committed
31 32 33
    );
  }

34 35 36 37 38 39
  protected function setUp() {
    parent::setUp();

    $this->enableViewsTestModule();
  }

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  /**
   * Overrides Drupal\views\Tests\ViewTestBase::viewsData().
   */
  protected function viewsData() {
    $data = parent::viewsData();
    // Override the name handler to be able to call placeholder() from outside.
    $data['views_test_data']['name']['field']['id'] = 'test_field';

    // Setup one field with an access callback and one with an access callback
    // and arguments.
    $data['views_test_data']['access_callback'] = $data['views_test_data']['id'];
    $data['views_test_data']['access_callback_arguments'] = $data['views_test_data']['id'];
    foreach (ViewExecutable::viewsHandlerTypes() as $type => $info) {
      if (isset($data['views_test_data']['access_callback'][$type]['id'])) {
        $data['views_test_data']['access_callback'][$type]['access callback'] = 'views_test_data_handler_test_access_callback';

        $data['views_test_data']['access_callback_arguments'][$type]['access callback'] = 'views_test_data_handler_test_access_callback_argument';
        $data['views_test_data']['access_callback_arguments'][$type]['access arguments'] = array(TRUE);
      }
    }

    return $data;
  }

  /**
   * @todo
   * This should probably moved to a filter related test.
   */
merlinofchaos's avatar
merlinofchaos committed
68 69 70
  function testFilterInOperatorUi() {
    $admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration'));
    $this->drupalLogin($admin_user);
71
    menu_router_rebuild();
merlinofchaos's avatar
merlinofchaos committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85

    $path = 'admin/structure/views/nojs/config-item/test_filter_in_operator_ui/default/filter/type';
    $this->drupalGet($path);
    $this->assertFieldByName('options[expose][reduce]', FALSE);

    $edit = array(
      'options[expose][reduce]' => TRUE,
    );
    $this->drupalPost($path, $edit, t('Apply'));
    $this->drupalGet($path);
    $this->assertFieldByName('options[expose][reduce]', TRUE);
  }

  /**
86
   * Tests the breakPhraseString() method.
merlinofchaos's avatar
merlinofchaos committed
87
   */
88
  function testBreakPhraseString() {
89
    $empty_stdclass = new \stdClass();
merlinofchaos's avatar
merlinofchaos committed
90 91 92 93
    $empty_stdclass->operator = 'or';
    $empty_stdclass->value = array();

    // check defaults
94 95
    $null = NULL;
    $this->assertEqual($empty_stdclass, HandlerBase::breakPhraseString('', $null));
merlinofchaos's avatar
merlinofchaos committed
96 97

    $handler = views_get_handler('node', 'title', 'argument');
98
    $this->assertEqual($handler, HandlerBase::breakPhraseString('', $handler), 'The breakPhraseString() method works correctly.');
merlinofchaos's avatar
merlinofchaos committed
99 100

    // test ors
101
    $handler = HandlerBase::breakPhraseString('word1 word2+word');
102
    $this->assertEqualValue(array('word1', 'word2', 'word'), $handler);
merlinofchaos's avatar
merlinofchaos committed
103
    $this->assertEqual('or', $handler->operator);
104
    $handler = HandlerBase::breakPhraseString('word1+word2+word');
105
    $this->assertEqualValue(array('word1', 'word2', 'word'), $handler);
merlinofchaos's avatar
merlinofchaos committed
106
    $this->assertEqual('or', $handler->operator);
107
    $handler = HandlerBase::breakPhraseString('word1 word2 word');
108
    $this->assertEqualValue(array('word1', 'word2', 'word'), $handler);
merlinofchaos's avatar
merlinofchaos committed
109
    $this->assertEqual('or', $handler->operator);
110
    $handler = HandlerBase::breakPhraseString('word-1+word-2+word');
111 112
    $this->assertEqualValue(array('word-1', 'word-2', 'word'), $handler);
    $this->assertEqual('or', $handler->operator);
113
    $handler = HandlerBase::breakPhraseString('wõrd1+wõrd2+wõrd');
114
    $this->assertEqualValue(array('wõrd1', 'wõrd2', 'wõrd'), $handler);
merlinofchaos's avatar
merlinofchaos committed
115 116 117
    $this->assertEqual('or', $handler->operator);

    // test ands.
118
    $handler = HandlerBase::breakPhraseString('word1,word2,word');
119 120
    $this->assertEqualValue(array('word1', 'word2', 'word'), $handler);
    $this->assertEqual('and', $handler->operator);
121
    $handler = HandlerBase::breakPhraseString('word1 word2,word');
122
    $this->assertEqualValue(array('word1 word2', 'word'), $handler);
merlinofchaos's avatar
merlinofchaos committed
123
    $this->assertEqual('and', $handler->operator);
124
    $handler = HandlerBase::breakPhraseString('word1,word2 word');
125
    $this->assertEqualValue(array('word1', 'word2 word'), $handler);
merlinofchaos's avatar
merlinofchaos committed
126
    $this->assertEqual('and', $handler->operator);
127
    $handler = HandlerBase::breakPhraseString('word-1,word-2,word');
128
    $this->assertEqualValue(array('word-1', 'word-2', 'word'), $handler);
merlinofchaos's avatar
merlinofchaos committed
129
    $this->assertEqual('and', $handler->operator);
130
    $handler = HandlerBase::breakPhraseString('wõrd1,wõrd2,wõrd');
131 132 133 134
    $this->assertEqualValue(array('wõrd1', 'wõrd2', 'wõrd'), $handler);
    $this->assertEqual('and', $handler->operator);

    // test a single word
135
    $handler = HandlerBase::breakPhraseString('word');
136
    $this->assertEqualValue(array('word'), $handler);
merlinofchaos's avatar
merlinofchaos committed
137 138 139 140
    $this->assertEqual('and', $handler->operator);
  }

  /**
141
   * Tests Drupal\views\Plugin\views\HandlerBase::breakPhrase() function.
merlinofchaos's avatar
merlinofchaos committed
142
   */
143
  function testBreakPhrase() {
144
    $empty_stdclass = new \stdClass();
merlinofchaos's avatar
merlinofchaos committed
145 146 147 148 149
    $empty_stdclass->operator = 'or';
    $empty_stdclass->value = array();

    $null = NULL;
    // check defaults
150
    $this->assertEqual($empty_stdclass, HandlerBase::breakPhrase('', $null));
merlinofchaos's avatar
merlinofchaos committed
151 152

    $handler = views_get_handler('node', 'title', 'argument');
153
    $this->assertEqual($handler, HandlerBase::breakPhrase('', $handler), 'The breakPhrase() method works correctly.');
merlinofchaos's avatar
merlinofchaos committed
154 155 156 157 158 159

    // Generate three random numbers which can be used below;
    $n1 = rand(0, 100);
    $n2 = rand(0, 100);
    $n3 = rand(0, 100);
    // test ors
160
    $this->assertEqualValue(array($n1, $n2, $n3), HandlerBase::breakPhrase("$n1 $n2+$n3", $handler));
merlinofchaos's avatar
merlinofchaos committed
161
    $this->assertEqual('or', $handler->operator);
162
    $this->assertEqualValue(array($n1, $n2, $n3), HandlerBase::breakPhrase("$n1+$n2+$n3", $handler));
merlinofchaos's avatar
merlinofchaos committed
163
    $this->assertEqual('or', $handler->operator);
164
    $this->assertEqualValue(array($n1, $n2, $n3), HandlerBase::breakPhrase("$n1 $n2 $n3", $handler));
merlinofchaos's avatar
merlinofchaos committed
165
    $this->assertEqual('or', $handler->operator);
166
    $this->assertEqualValue(array($n1, $n2, $n3), HandlerBase::breakPhrase("$n1 $n2++$n3", $handler));
merlinofchaos's avatar
merlinofchaos committed
167 168 169
    $this->assertEqual('or', $handler->operator);

    // test ands.
170
    $this->assertEqualValue(array($n1, $n2, $n3), HandlerBase::breakPhrase("$n1,$n2,$n3", $handler));
merlinofchaos's avatar
merlinofchaos committed
171
    $this->assertEqual('and', $handler->operator);
172
    $this->assertEqualValue(array($n1, $n2, $n3), HandlerBase::breakPhrase("$n1,,$n2,$n3", $handler));
merlinofchaos's avatar
merlinofchaos committed
173 174 175 176
    $this->assertEqual('and', $handler->operator);
  }

  /**
177
   * Check to see if a value is the same as the value on a certain handler.
merlinofchaos's avatar
merlinofchaos committed
178 179 180
   *
   * @param $first
   *   The first value to check.
181 182
   * @param Drupal\views\Plugin\views\HandlerBase $handler
   *   The handler that has the $handler->value property to compare with first.
merlinofchaos's avatar
merlinofchaos committed
183 184 185 186 187 188 189 190 191 192 193
   * @param string $message
   *   The message to display along with the assertion.
   * @param string $group
   *   The type of assertion - examples are "Browser", "PHP".
   *
   * @return bool
   *   TRUE if the assertion succeeded, FALSE otherwise.
   */
  protected function assertEqualValue($first, $handler, $message = '', $group = 'Other') {
    return $this->assert($first == $handler->value, $message ? $message : t('First value is equal to second value'), $group);
  }
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
  /**
   * Tests the relationship ui for field/filter/argument/relationship.
   */
  public function testRelationshipUI() {
    $views_admin = $this->drupalCreateUser(array('administer views'));
    $this->drupalLogin($views_admin);

    // Make sure the link to the field options exists.
    $handler_options_path = 'admin/structure/views/nojs/config-item/test_handler_relationships/default/field/title';
    $view_edit_path = 'admin/structure/views/view/test_handler_relationships/edit';
    $this->drupalGet($view_edit_path);
    $this->assertLinkByHref($handler_options_path);

    // The test view has a relationship to node_revision so the field should
    // show a relationship selection.

    $this->drupalGet($handler_options_path);
    $relationship_name = 'options[relationship]';
    $this->assertFieldByName($relationship_name);

    // Check for available options.
    $xpath = $this->constructFieldXpath('name', $relationship_name);
    $fields = $this->xpath($xpath);
    $options = array();
    foreach ($fields as $field) {
      $items = $this->getAllOptions($field);
      foreach ($items as $item) {
        $options[] = $item->attributes()->value;
      }
    }
    $expected_options = array('none', 'nid');
    $this->assertEqual($options, $expected_options);

    // Remove the relationship and take sure no relationship option appears.
    $this->drupalPost('admin/structure/views/nojs/config-item/test_handler_relationships/default/relationship/nid', array(), t('Remove'));
    $this->drupalGet($handler_options_path);
    $this->assertNoFieldByName($relationship_name, 'Make sure that no relationship option is available');
  }

  /**
   * Tests the relationship method on the base class.
   */
  public function testSetRelationship() {
    $view = $this->createViewFromConfig('test_handler_relationships');
    // Setup a broken relationship.
    $view->addItem('default', 'relationship', $this->randomName(), $this->randomName(), array(), 'broken_relationship');
    // Setup a valid relationship.
    $view->addItem('default', 'relationship', 'comment', 'nid', array('relationship' => 'cid'), 'valid_relationship');
    $view->initHandlers();
    $field = $view->field['title'];

    $field->options['relationship'] = NULL;
    $field->setRelationship();
    $this->assertFalse($field->relationship, 'Make sure that an empty relationship does not create a relationship on the field.');

    $field->options['relationship'] = $this->randomName();
    $field->setRelationship();
    $this->assertFalse($field->relationship, 'Make sure that a random relationship does not create a relationship on the field.');

    $field->options['relationship'] = 'broken_relationship';
    $field->setRelationship();
    $this->assertFalse($field->relationship, 'Make sure that a broken relationship does not create a relationship on the field.');

    $field->options['relationship'] = 'valid_relationship';
    $field->setRelationship();
    $this->assertFalse(!empty($field->relationship), 'Make sure that the relationship alias was not set without building a views query before.');

    // Remove the invalid relationship.
    unset($view->relationship['broken_relationship']);

    $view->build();
    $field->setRelationship();
    $this->assertEqual($field->relationship, $view->relationship['valid_relationship']->alias, 'Make sure that a valid relationship does create the right relationship query alias.');
  }

  /**
   * Tests the placeholder function.
   *
   * @see Drupal\views\Plugin\views\HandlerBase::placeholder()
   */
  public function testPlaceholder() {
    $view = $this->getView();
    $view->initHandlers();
    $view->initQuery();

    $handler = $view->field['name'];
    $table = $handler->table;
    $field = $handler->field;
    $string = ':' . $table . '_' . $field;

    // Make sure the placeholder variables are like expected.
    $this->assertEqual($handler->getPlaceholder(), $string);
    $this->assertEqual($handler->getPlaceholder(), $string . 1);
    $this->assertEqual($handler->getPlaceholder(), $string . 2);

    // Set another table/field combination and make sure there are new
    // placeholders.
    $table = $handler->table = $this->randomName();
    $field = $handler->field = $this->randomName();
    $string = ':' . $table . '_' . $field;

    // Make sure the placeholder variables are like expected.
    $this->assertEqual($handler->getPlaceholder(), $string);
    $this->assertEqual($handler->getPlaceholder(), $string . 1);
    $this->assertEqual($handler->getPlaceholder(), $string . 2);
  }

  /**
   * Tests access to a handler.
   *
   * @see views_test_data_handler_test_access_callback
   */
  public function testAccess() {
    $view = views_get_view('test_handler_test_access');
    $views_data = $this->viewsData();
    $views_data = $views_data['views_test_data'];

    // Enable access to callback only field and deny for callback + arguments.
    config('views_test_data.tests')->set('handler_access_callback', TRUE)->save();
    config('views_test_data.tests')->set('handler_access_callback_argument', FALSE)->save();
    $view->initDisplay();
    $view->initHandlers();

    foreach ($views_data['access_callback'] as $type => $info) {
      if (!in_array($type, array('title', 'help'))) {
        $this->assertTrue($view->field['access_callback'] instanceof HandlerBase, 'Make sure the user got access to the access_callback field ');
        $this->assertFalse(isset($view->field['access_callback_arguments']), 'Make sure the user got no access to the access_callback_arguments field ');
      }
    }

    // Enable access to the callback + argument handlers and deny for callback.
    config('views_test_data.tests')->set('handler_access_callback', FALSE)->save();
    config('views_test_data.tests')->set('handler_access_callback_argument', TRUE)->save();
    $view->destroy();
    $view->initDisplay();
    $view->initHandlers();

    foreach ($views_data['access_callback'] as $type => $info) {
      if (!in_array($type, array('title', 'help'))) {
        $this->assertFalse(isset($view->field['access_callback']), 'Make sure the user got no access to the access_callback field ');
        $this->assertTrue($view->field['access_callback_arguments'] instanceof HandlerBase, 'Make sure the user got access to the access_callback_arguments field ');
      }
    }
  }

merlinofchaos's avatar
merlinofchaos committed
340
}