Projet

Général

Profil

Paste
Télécharger (33,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds_tamper / tests / feeds_tamper_plugins.test @ d808fa20

1
<?php
2

    
3
/**
4
 * @file
5
 * Unit tests for feeds tamper plugins.
6
 *
7
 * @todo
8
 * Check for form errors in validation.
9
 */
10

    
11
/**
12
 * Base class for plugin unit tests.
13
 */
14
class FeedsTamperUnitTestCase extends DrupalUnitTestCase {
15
  protected $plugin_id = '';
16

    
17
  public function setUp() {
18
    parent::setUp();
19
    $plugin = NULL;
20
    $file = drupal_get_path('module', 'feeds_tamper') . '/plugins/' . $this->plugin_id . '.inc';
21
    require_once $file;
22
    $this->plugin_info = $plugin;
23
  }
24

    
25
  protected function validate(&$settings = array()) {
26
    if (!empty($this->plugin_info['validate'])) {
27
      $this->plugin_info['validate']($settings);
28
      if ($errors = form_get_errors()) {
29
        foreach ($errors as $key => $value) {
30
          $this->error(t('Input error on %key with value %value.', array('%key' => $key, '%value' => $value)));
31
        }
32
      }
33
    }
34
  }
35

    
36
  protected function callback($result, $item_key, $element_key, &$field, $settings = array(), $source = NULL) {
37
    if (is_null($source)) {
38
      $source = new stdClass();
39
    }
40
    $this->plugin_info['callback']($result, $item_key, $element_key, $field, $settings, $source);
41
  }
42

    
43
  protected function execute($input, $output, $settings = array()) {
44
    $this->validate($settings);
45
    $this->callback(NULL, NULL, NULL, $input, $settings);
46
    $this->assertEqual($input, $output);
47
  }
48

    
49
}
50

    
51
/**
52
 * Tests for absolute_url.inc
53
 */
54
class FeedsTamperAbsoluteURLTestCase extends FeedsTamperUnitTestCase {
55

    
56
  protected $plugin_id = 'absolute_url';
57

    
58
  public static function getInfo() {
59
    return array(
60
      'name' => 'Plugins: Make URLs absolute',
61
      'description' => 'Unit tests for "Make URLs absolute" plugin.',
62
      'group' => 'Feeds Tamper',
63
    );
64
  }
65

    
66
  public function test() {
67
    $this->executeAbs('http://example.com', '<a href="dog"></a>', '<a href="http://example.com/dog"></a>');
68
    $this->executeAbs('http://example.com/cat/chicken', '<a href="dog"></a>', '<a href="http://example.com/cat/chicken/dog"></a>');
69
    $this->executeAbs('http://example.com/cat', '<a href="/dog"></a>', '<a href="http://example.com/dog"></a>');
70
    $this->executeAbs('http://example.com', '<a href="/dog"></a><img src="/kitty" />', '<a href="http://example.com/dog"></a><img src="http://example.com/kitty" />');
71
    $this->executeAbs('http://example.com', '<img src="/kitty" />', '<img src="http://example.com/kitty" />');
72
    $this->executeAbs('http://example.com', '<img src="kitty" />', '<img src="http://example.com/kitty" />');
73
    $this->executeAbs('http://example.com', '<img src="/kitty.png" />', '<img src="http://example.com/kitty.png" />');
74
    $this->executeAbs('http://example.com', '<img src="/frog/kitty.png" />', '<img src="http://example.com/frog/kitty.png" />');
75
    $this->executeAbs('http://example.com', '', '');
76
    $this->executeAbs('http://example.com', 'sdfsdfdsf', 'sdfsdfdsf');
77
    $this->executeAbs('http://example.com', '<a href="">asdfasdf</a>', '<a href="">asdfasdf</a>');
78
  }
79

    
80
  public function executeAbs($link, $html_in, $html_out) {
81
    $result = new stdClass();
82
    $result->link = $link;
83
    $this->callback($result, NULL, NULL, $html_in, array());
84
    $this->assertEqual($html_in, $html_out);
85
  }
86

    
87
}
88

    
89
/**
90
 * Tests for array_filter.inc
91
 */
92
class FeedsTamperArrayFilterTestCase extends FeedsTamperUnitTestCase {
93

    
94
  protected $plugin_id = 'array_filter';
95

    
96
  public static function getInfo() {
97
    return array(
98
      'name' => 'Plugins: Filter empty items',
99
      'description' => 'Unit tests for "Filter empty items".',
100
      'group' => 'Feeds Tamper',
101
    );
102
  }
103

    
104
  public function test() {
105
    $this->executeFilter(array('asdf', 0, FALSE, '', 1234), array('asdf', 1234));
106
  }
107

    
108
  protected function executeFilter($in_array, $out_array) {
109
    $this->callback(NULL, NULL, NULL, $in_array);
110
    $this->assertEqual($in_array, $out_array);
111
  }
112

    
113
}
114

    
115
/**
116
 * Tests for absolute_url.inc
117
 */
118
class FeedsTamperCastToIntTestCase extends FeedsTamperUnitTestCase {
119

    
120
  protected $plugin_id = 'cast_to_int';
121

    
122
  public static function getInfo() {
123
    return array(
124
      'name' => 'Plugins: Cast to int',
125
      'description' => 'Unit tests for "Cast to integer" plugin.',
126
      'group' => 'Feeds Tamper',
127
    );
128
  }
129

    
130
  public function test() {
131
    $this->execute('1', 1);
132
    $this->execute('asdfsdf', 0);
133
    $this->execute('1.2324', 1);
134
    $this->execute(1.234, 1);
135
    $this->execute(TRUE, 1);
136
    $this->execute(FALSE, 0);
137
    $this->execute('23456', 23456);
138
  }
139

    
140
  protected function executeCastToInt($input, $output, $settings = array()) {
141
    $this->validate($settings);
142
    $this->callback(NULL, NULL, NULL, $input, $settings);
143
    $this->assertIdentical($input, $output);
144
  }
145

    
146
}
147

    
148
/**
149
 * Tests for convert_boolean.inc
150
 */
151
class FeedsTamperCovertBooleanTestCase extends FeedsTamperUnitTestCase {
152

    
153
  protected $plugin_id = 'convert_boolean';
154

    
155
  public static function getInfo() {
156
    return array(
157
      'name' => 'Plugins: Convert to boolean',
158
      'description' => 'Unit tests for "Convert to boolean" plugin.',
159
      'group' => 'Feeds Tamper',
160
    );
161
  }
162

    
163
  public function test() {
164
    $settings = array(
165
      'true_value' => 'A',
166
      'false_value' => 'B',
167
      'match_case' => FALSE,
168
      'no_match' => 'false',
169
    );
170
    // Basic functionality.
171
    $this->execute('A', TRUE, $settings);
172
    $this->execute('B', FALSE, $settings);
173
    // match_case = FALSE works.
174
    $this->execute('a', TRUE, $settings);
175
    $this->execute('b', FALSE, $settings);
176
    // no_match = false
177
    $this->execute('c', FALSE, $settings);
178
    $settings['no_match'] = 'pass';
179
    $this->execute('c', 'c', $settings);
180
    // match_case = TRUE
181
    $settings['match_case'] = TRUE;
182
    $settings['no_match'] = 'false';
183
    $this->execute('a', FALSE, $settings);
184
    // no_match = NULL.
185
    $settings['no_match'] = 'null';
186
    $this->execute('a', NULL, $settings);
187
    // other_text = 'other text'.
188
    $settings['no_match'] = 'other';
189
    $settings['other_text'] = 'other text';
190
    $this->execute('a', 'other text', $settings);
191
  }
192

    
193
}
194

    
195
/**
196
 * Tests for convert_case.inc
197
 */
198
class FeedsTamperConvertCaseTestCase extends FeedsTamperUnitTestCase {
199

    
200
  protected $plugin_id = 'convert_case';
201

    
202
  public static function getInfo() {
203
    return array(
204
      'name' => 'Plugins: Convert Case',
205
      'description' => 'Unit tests for "Convert Case" plugin.',
206
      'group' => 'Feeds Tamper',
207
    );
208
  }
209

    
210
  public function test() {
211
    $this->execute('asdfasdf', 'ASDFASDF', array('mode' => MB_CASE_UPPER));
212
    $this->execute('AsdFasdf', 'asdfasdf', array('mode' => MB_CASE_LOWER));
213
    $this->execute('asdfasdf', 'Asdfasdf', array('mode' => MB_CASE_TITLE));
214
  }
215

    
216
}
217

    
218
/**
219
 * Tests for copy.inc
220
 */
221
class FeedsTamperCopyTestCase extends FeedsTamperUnitTestCase {
222

    
223
  protected $plugin_id = 'copy';
224

    
225
  public static function getInfo() {
226
    return array(
227
      'name' => 'Plugins: Copy',
228
      'description' => 'Unit tests for "Copy" plugin.',
229
      'group' => 'Feeds Tamper',
230
    );
231
  }
232

    
233
  public function test() {
234
    $settings = array(
235
      'to_from' => 'to',
236
      'source' => 'body',
237
    );
238
    $item = array(
239
      'title' => 'This is a title',
240
      'body' => 'This is a body',
241
    );
242
    $this->executeCop($item, 'title', $settings);
243
    $settings = array(
244
      'to_from' => 'from',
245
      'source' => 'body',
246
    );
247
    $this->executeCop($item, 'title', $settings);
248
  }
249

    
250
  protected function executeCop($input, $element_key, $settings) {
251
    $result = new stdClass();
252
    $result->items = array();
253
    $result->items[] = $input;
254
    $this->callback($result, 0, $element_key, $result->items[0][$element_key], $settings);
255
    $this->assertEqual($result->items[0][$element_key], $result->items[0][$settings['source']]);
256
  }
257

    
258
}
259

    
260

    
261
/**
262
 * Tests for country_to_code.inc
263
 */
264
class FeedsTamperCountryToCodeTestCase extends FeedsTamperUnitTestCase {
265

    
266
  protected $plugin_id = 'country_to_code';
267

    
268
  public static function getInfo() {
269
    return array(
270
      'name' => 'Plugins: Country to Code',
271
      'description' => 'Unit tests for "Country to Code" plugin.',
272
      'group' => 'Feeds Tamper',
273
    );
274
  }
275

    
276
  public function test() {
277
    $this->execute('Argentina', 'AR');
278
    // Test that lowercasing and trimming works.
279
    $this->execute('antigua and barbuda ', 'AG');
280
  }
281

    
282
}
283

    
284
/**
285
 * Tests for default_value.inc
286
 */
287
class FeedsTamperDefaultValueTestCase extends FeedsTamperUnitTestCase {
288

    
289
  protected $plugin_id = 'default_value';
290

    
291
  public static function getInfo() {
292
    return array(
293
      'name' => 'Plugins: Set default value',
294
      'description' => 'Unit tests for "Set default value" plugin.',
295
      'group' => 'Feeds Tamper',
296
    );
297
  }
298

    
299
  public function test() {
300
    $this->execute('asdfasdf', 'HELLO', array('default_value' => 'HELLO'));
301
    $this->execute(array('asdfasdf'), 'HELLO', array('default_value' => 'HELLO'));
302

    
303
    // Test default value.
304
    $this->execute(array(), 'HELLO', array('default_value' => 'HELLO', 'only_if_empty' => TRUE));
305
    $this->execute(array(1), array(1), array('default_value' => 'HELLO', 'only_if_empty' => TRUE));
306
  }
307

    
308
}
309

    
310
/**
311
 * Tests for explode.inc
312
 */
313
class FeedsTamperExplodeTestCase extends FeedsTamperUnitTestCase {
314

    
315
  protected $plugin_id = 'explode';
316

    
317
  public static function getInfo() {
318
    return array(
319
      'name' => 'Plugins: Explode',
320
      'description' => 'Unit tests for "Explode" plugin.',
321
      'group' => 'Feeds Tamper',
322
    );
323
  }
324

    
325
  public function test() {
326
    $settings = array(
327
      'separator' => ',',
328
      'limit' => NULL,
329
    );
330
    $this->execute('a,b,c,d', array('a', 'b', 'c', 'd'), $settings);
331
    $this->execute('1,2,3,4', array('1', '2', '3', '4'), $settings);
332
    $this->execute('123,1.23,abc,456 ,789,def890', array('123', '1.23', 'abc', '456 ', '789', 'def890'), $settings);
333
    $settings['limit'] = 2;
334
    $this->execute('a,b,c,d', array('a', 'b,c,d'), $settings);
335
    $this->execute('a.b.c.d', array('a.b.c.d'), $settings);
336
    // Test multiple value handling.
337
    $settings['limit'] = NULL;
338
    $this->execute(array('a,b,c,d', 'e,f,g,h'), array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'), $settings);
339
    $this->execute(array('abcd', 'e,f,g,h'), array('abcd', 'e', 'f', 'g', 'h'), $settings);
340
    // Test when separator is not present.
341
    $this->execute('abcd', array('abcd'), $settings);
342
    // Test negative values.
343
    //$settings['limit'] = 2;
344
    //$this->execute('a,b,c,d', array('a', 'b,c,d'), $settings);
345
    //$this->execute('a.b.c.d', array('a.b.c.d'), $settings);
346
  }
347

    
348
}
349

    
350
/**
351
 * Tests for find_replace.inc
352
 */
353
class FeedsTamperFindReplaceTestCase extends FeedsTamperUnitTestCase {
354

    
355
  protected $plugin_id = 'find_replace';
356

    
357
  public static function getInfo() {
358
    return array(
359
      'name' => 'Plugins: Find Replace',
360
      'description' => 'Unit tests for "Find Replace" plugin.',
361
      'group' => 'Feeds Tamper',
362
    );
363
  }
364

    
365
  public function test() {
366
    $settings = array(
367
      'find' => 'cat',
368
      'replace' => 'dog',
369
      'case_sensitive' => FALSE,
370
      'word_boundaries' => FALSE,
371
      'whole' => FALSE,
372
    );
373
    $this->execute('The cat went to the park.', 'The dog went to the park.', $settings);
374
    $this->execute('The Cat went to the park.', 'The dog went to the park.', $settings);
375
    $this->execute('The Catwent to the park.', 'The dogwent to the park.', $settings);
376
    $settings = array(
377
      'find' => 'cat',
378
      'replace' => 'dog',
379
      'case_sensitive' => TRUE,
380
      'word_boundaries' => FALSE,
381
      'whole' => FALSE,
382
    );
383
    $this->execute('The cat went to the park.', 'The dog went to the park.', $settings);
384
    $this->execute('The Cat went to the park.', 'The Cat went to the park.', $settings);
385
    $this->execute('The catwent to the park.', 'The dogwent to the park.', $settings);
386
    $settings = array(
387
      'find' => 'c/at',
388
      'replace' => 'dog',
389
      'case_sensitive' => FALSE,
390
      'word_boundaries' => TRUE,
391
      'whole' => FALSE,
392
    );
393
    $this->execute('The c/at went to the park.', 'The dog went to the park.', $settings);
394
    $this->execute('The C/at went to the park.', 'The dog went to the park.', $settings);
395
    $this->execute('The c/atwent to the park.', 'The c/atwent to the park.', $settings);
396
    $settings = array(
397
      'find' => '/cat',
398
      'replace' => 'dog',
399
      'case_sensitive' => FALSE,
400
      'word_boundaries' => FALSE,
401
      'whole' => TRUE,
402
    );
403
    $this->execute('The /cat went to the park.', 'The /cat went to the park.', $settings);
404
    $this->execute('/cat', 'dog', $settings);
405
    $this->execute('/Cat', 'dog', $settings);
406
  }
407

    
408
}
409

    
410
/**
411
 * Tests for find_replace_regex.inc
412
 */
413
class FeedsTamperFindReplaceREGEXTestCase extends FeedsTamperUnitTestCase {
414

    
415
  protected $plugin_id = 'find_replace_regex';
416

    
417
  public static function getInfo() {
418
    return array(
419
      'name' => 'Plugins: Find Replace Regex',
420
      'description' => 'Unit tests for "Find Replace Regex" plugin.',
421
      'group' => 'Feeds Tamper',
422
    );
423
  }
424

    
425
  public function test() {
426
    $settings = array('find' => '/cat/', 'replace' => 'dog', 'limit' => '');
427
    $this->execute('The cat went to the park.', 'The dog went to the park.', $settings);
428

    
429
    $settings['find'] = '/cat/i';
430
    $this->execute('The Cat went to the park.', 'The dog went to the park.', $settings);
431

    
432
    $settings['find'] = '/cat\b/i';
433
    $this->execute('The Catwent to the park.', 'The Catwent to the park.', $settings);
434

    
435
    $settings['find'] = '/cat\n/';
436
    $this->execute("The cat\n went to the park.", 'The dog went to the park.', $settings);
437

    
438
    $settings['find'] = '/cat\s/';
439
    $this->execute("The cat\n went to the park.", 'The dog went to the park.', $settings);
440

    
441
    $settings['find'] = '/cat\r\n/';
442
    $this->execute("The cat\r\n went to the park.", 'The dog went to the park.', $settings);
443

    
444
    $settings['find'] = '/cat\t/';
445
    $this->execute("The cat\t went to the park.", 'The dog went to the park.', $settings);
446
  }
447

    
448
}
449

    
450
/**
451
 * Tests for hash.inc
452
 */
453
class FeedsTamperHashTestCase extends FeedsTamperUnitTestCase {
454

    
455
  protected $plugin_id = 'hash';
456

    
457
  public static function getInfo() {
458
    return array(
459
      'name' => 'Plugins: Hash',
460
      'description' => 'Unit tests for "Calculate hash" plugin.',
461
      'group' => 'Feeds Tamper',
462
    );
463
  }
464

    
465
  public function test() {
466
    $item = array('title' => 'Yay title!', 'body' => 'Yay body!', 'hash' => 'asdfasf');
467
    $settings = array('override' => TRUE);
468
    $this->executeHash($item, md5(serialize($item)), $settings);
469
    // Test override = FALSE.
470
    $settings['override'] = FALSE;
471
    $this->executeHash($item, 'asdfasf', $settings);
472
  }
473
  public function executeHash($input, $output, $settings) {
474
    $result = new stdClass();
475
    $result->items = array();
476
    $result->items[] = $input;
477
    $this->callback($result, 0, 'hash', $result->items[0]['hash'], $settings);
478
    $this->assertIdentical($result->items[0]['hash'], $output);
479
  }
480

    
481
}
482

    
483
/**
484
 * Tests for html_entity_encode.inc
485
 */
486
class FeedsTamperHTMLEntityEncodeTestCase extends FeedsTamperUnitTestCase {
487

    
488
  protected $plugin_id = 'html_entity_encode';
489

    
490
  public static function getInfo() {
491
    return array(
492
      'name' => 'Plugins: HTML Entity Encode',
493
      'description' => 'Unit tests for "HTML Entity Encode" plugin.',
494
      'group' => 'Feeds Tamper',
495
    );
496
  }
497

    
498
  public function test() {
499
    $this->execute('<html>asdfsadfasf<b>asfasf</b></html>', '&lt;html&gt;asdfsadfasf&lt;b&gt;asfasf&lt;/b&gt;&lt;/html&gt;');
500
  }
501

    
502
}
503

    
504
/**
505
 * Tests for html_entity_decode.inc
506
 */
507
class FeedsTamperHTMLEntityDecodeTestCase extends FeedsTamperUnitTestCase {
508

    
509
  protected $plugin_id = 'html_entity_decode';
510

    
511
  public static function getInfo() {
512
    return array(
513
      'name' => 'Plugins: HTML Entity Decode',
514
      'description' => 'Unit tests for "HTML Entity Decode" plugin.',
515
      'group' => 'Feeds Tamper',
516
    );
517
  }
518

    
519
  public function test() {
520
    $this->execute('&lt;html&gt;asdfsadfasf&lt;b&gt;asfasf&lt;/b&gt;&lt;/html&gt;', '<html>asdfsadfasf<b>asfasf</b></html>');
521
  }
522

    
523
}
524

    
525
/**
526
 * Tests for implode.inc
527
 */
528
class FeedsTamperImplodeTestCase extends FeedsTamperUnitTestCase {
529

    
530
  protected $plugin_id = 'implode';
531

    
532
  public static function getInfo() {
533
    return array(
534
      'name' => 'Plugins: Implode',
535
      'description' => 'Unit tests for "Implode" plugin.',
536
      'group' => 'Feeds Tamper',
537
    );
538
  }
539

    
540
  public function test() {
541
    $settings = array(
542
      'glue' => ',',
543
    );
544
    $this->execute(array('a', 'b', 'c'), 'a,b,c', $settings);
545
    $settings = array(
546
      'glue' => ',%s',
547
    );
548
    $this->execute(array('a', 'b', 'c'), 'a, b, c', $settings);
549
    $settings = array(
550
      'glue' => ',%t',
551
    );
552
    $this->execute(array('a', 'b', 'c'), "a,\tb,\tc", $settings);
553
    $settings = array(
554
      'glue' => ',%n',
555
    );
556
    $this->execute(array('a', 'b', 'c'), "a,\nb,\nc", $settings);
557
  }
558

    
559
}
560

    
561
/**
562
 * Tests for keyword_filter.inc
563
 */
564
class FeedsTamperKeyWordFilterTestCase extends FeedsTamperUnitTestCase {
565

    
566
  protected $plugin_id = 'keyword_filter';
567

    
568
  public static function getInfo() {
569
    return array(
570
      'name' => 'Plugins: Keyword Filter',
571
      'description' => 'Unit tests for "Keyword Filter" plugin.',
572
      'group' => 'Feeds Tamper',
573
    );
574
  }
575

    
576
  public function test() {
577
    $item = array(
578
      'title' => 'This is a title',
579
      'body' => 'hello body',
580
    );
581

    
582
    // Test stripos(), filter.
583
    $settings = array(
584
      'words' => 'booya',
585
      'word_boundaries' => FALSE,
586
      'case_sensitive' => FALSE,
587
    );
588
    $this->executeKey($item, array(), 'title', $settings);
589

    
590
    // Test stripos(), pass.
591
    $settings = array(
592
      'words' => 'this',
593
      'word_boundaries' => FALSE,
594
      'case_sensitive' => FALSE,
595
    );
596

    
597
    $this->executeKey($item, array($item), 'title', $settings);
598

    
599
    // Test strpos(), filter.
600
    $settings = array(
601
      'words' => 'this',
602
      'word_boundaries' => FALSE,
603
      'case_sensitive' => TRUE,
604
    );
605

    
606
    $this->executeKey($item, array(), 'title', $settings);
607

    
608
    // Test exact, filter.
609
    $settings = array(
610
      'words' => 'a title',
611
      'word_boundaries' => TRUE,
612
      'case_sensitive' => FALSE,
613
      'exact' => TRUE,
614
    );
615

    
616
    $this->executeKey($item, array(), 'title', $settings);
617

    
618
    // Test exact, filter.
619
    $settings = array(
620
      'words' => 'This is a Title',
621
      'case_sensitive' => FALSE,
622
      'exact' => TRUE,
623
    );
624
    $this->executeKey($item, array($item), 'title', $settings);
625

    
626
    // Test word boundaries.
627
    $item = array(
628
      'title' => 'This is atitle',
629
      'body' => 'hello body',
630
    );
631

    
632
    $settings = array(
633
      'words' => 'title',
634
      'word_boundaries' => TRUE,
635
      'case_sensitive' => FALSE,
636
      'invert' => FALSE,
637
    );
638
    $this->executeKey($item, array(), 'title', $settings);
639

    
640
    // Test invert = TRUE.
641
    $item = array(
642
      'title' => 'This is a title',
643
      'body' => 'hello body',
644
    );
645

    
646
    // Test stripos() pass.
647
    $settings = array(
648
      'words' => 'booya',
649
      'word_boundaries' => FALSE,
650
      'case_sensitive' => FALSE,
651
      'invert' => TRUE,
652
    );
653
    $this->executeKey($item, array($item), 'title', $settings);
654

    
655
    // Test stripos() filter.
656
    $settings = array(
657
      'words' => 'this',
658
      'word_boundaries' => FALSE,
659
      'case_sensitive' => FALSE,
660
      'invert' => TRUE,
661
    );
662
    $this->executeKey($item, array(), 'title', $settings);
663

    
664
    // Test strpos(), pass.
665
    $settings = array(
666
      'words' => 'this',
667
      'word_boundaries' => FALSE,
668
      'case_sensitive' => TRUE,
669
      'invert' => TRUE,
670
    );
671
    $this->executeKey($item, array($item), 'title', $settings);
672

    
673
    // Test exact, pass.
674
    $settings = array(
675
      'words' => 'a title',
676
      'word_boundaries' => TRUE,
677
      'case_sensitive' => FALSE,
678
      'invert' => TRUE,
679
      'exact' => TRUE,
680
    );
681
    $this->executeKey($item, array($item), 'title', $settings);
682

    
683
    // Test exact, filter.
684
    $settings = array(
685
      'words' => 'This is a title',
686
      'word_boundaries' => TRUE,
687
      'case_sensitive' => FALSE,
688
      'invert' => TRUE,
689
      'exact' => TRUE,
690
    );
691
    $this->executeKey($item, array(), 'title', $settings);
692

    
693
    // Test word boundaries, pass.
694
    $item = array(
695
      'title' => 'This is atitle',
696
      'body' => 'hello body',
697
    );
698

    
699
    $settings = array(
700
      'words' => 'title',
701
      'word_boundaries' => TRUE,
702
      'case_sensitive' => FALSE,
703
      'invert' => TRUE,
704
    );
705
    $this->executeKey($item, array($item), 'title', $settings);
706
  }
707

    
708
  public function executeKey($item, $output, $element_key, $settings) {
709
    $result = new stdClass();
710
    $result->items = array();
711
    $result->items[] = $item;
712

    
713
    $this->validate($settings);
714

    
715
    $this->callback($result, 0, NULL, $result->items[0][$element_key], $settings);
716
    $this->assertEqual($result->items, $output);
717

    
718
    // Test multi valued.
719
    foreach ($item as $key => $value) {
720
      $item[$key] = array($value);
721
    }
722

    
723
    if (!empty($output)) {
724
      foreach ($output[0] as $key => $value) {
725
        $output[0][$key] = array($value);
726
      }
727
    }
728

    
729
    $result = new stdClass();
730
    $result->items = array();
731
    $result->items[] = $item;
732

    
733
    $this->callback($result, 0, NULL, $result->items[0][$element_key], $settings);
734
    $this->assertEqual($result->items, $output);
735
  }
736

    
737
}
738

    
739
/**
740
 * Tests for math.inc
741
 */
742
class FeedsTamperMathTestCase extends FeedsTamperUnitTestCase {
743

    
744
  protected $plugin_id = 'math';
745

    
746
  public static function getInfo() {
747
    return array(
748
      'name' => 'Plugins: Math',
749
      'description' => 'Unit tests for "Math" plugin.',
750
      'group' => 'Feeds Tamper',
751
    );
752
  }
753

    
754
  public function test() {
755
    $settings = array(
756
      'operation' => 'addition',
757
      'value' => '2',
758
    );
759

    
760
    $this->execute(2, 4, $settings);
761

    
762
    // Test TRUE, FALSE, and NULL.
763
    $this->execute(TRUE, 3, $settings);
764
    $this->execute(FALSE, 2, $settings);
765
    $this->execute(NULL, 2, $settings);
766

    
767
    $settings['operation'] = 'subtraction';
768
    $this->execute(2, 0, $settings);
769

    
770
    $settings['operation'] = 'multiplication';
771
    $this->execute(2, 4, $settings);
772

    
773
    $settings['operation'] = 'division';
774
    $this->execute(2, 1, $settings);
775

    
776
    // Test flip.
777
    $settings['flip'] = TRUE;
778
    $settings['value'] = 3;
779

    
780
    $settings['operation'] = 'division';
781
    $this->execute(2, 3 / 2, $settings);
782

    
783
    $settings['operation'] = 'subtraction';
784
    $this->execute(2, 1, $settings);
785

    
786
    // Test invalid value.
787
    $this->execute('boo', 'boo', $settings);
788
  }
789

    
790
}
791

    
792
/**
793
 * Tests for number_format.inc
794
 */
795
class FeedsTamperNumberFormatTestCase extends FeedsTamperUnitTestCase {
796

    
797
  protected $plugin_id = 'number_format';
798

    
799
  public static function getInfo() {
800
    return array(
801
      'name' => 'Plugins: Format a number',
802
      'description' => 'Unit tests for "Format a number" plugin.',
803
      'group' => 'Feeds Tamper',
804
    );
805
  }
806

    
807
  public function test() {
808
    $settings = array(
809
      'decimals' => '0',
810
      'dec_point' => '.',
811
      'thousands_sep' => ',',
812
    );
813
    $num = '1234.56';
814
    $this->execute($num, '1,235', $settings);
815
    // French notation.
816
    $settings['decimals'] = '2';
817
    $settings['thousands_sep'] = ' ';
818
    $settings['dec_point'] = ',';
819
    $this->execute($num, '1 234,56', $settings);
820
    $num = 1234.5678;
821
    $settings['thousands_sep'] = '';
822
    $settings['dec_point'] = '.';
823
    $this->execute($num, '1234.57', $settings);
824
  }
825

    
826
}
827

    
828
/**
829
 * Tests for required.inc
830
 */
831
class FeedsTamperRequiredTestCase extends FeedsTamperUnitTestCase {
832

    
833
  protected $plugin_id = 'required';
834

    
835
  public static function getInfo() {
836
    return array(
837
      'name' => 'Plugins: Required',
838
      'description' => 'Unit tests for "Required" plugin.',
839
      'group' => 'Feeds Tamper',
840
    );
841
  }
842

    
843
  public function executeReq(array $input, array $output, $source, $settings = array()) {
844
    $result = new stdClass();
845
    $result->items = $input;
846
    foreach ($result->items as $key => &$item) {
847
      foreach ($item as $element_key => &$i) {
848
        if ($source == $element_key) {
849
          $this->callback($result, $key, $element_key, $i, $settings);
850
        }
851
      }
852
    }
853
    $this->assertEqual($result->items, $output);
854
  }
855

    
856
  public function test() {
857
    $input = array();
858
    $input[] = array('s1' => 'sdafasf', 's2' => 'asdfsf', 's3' => 'asdfasf');
859
    $input[] = array('s1' => 'sdafasf', 's2' => 'asdfsf', 's3' => NULL);
860
    $input[] = array('s1' => 'sdafasf', 's2' => 'asdfsf', 's3' => 'asdfasf');
861
    $output = $input;
862
    unset($output[1]);
863
    $this->executeReq($input, $output, 's3');
864

    
865
    // Test inverted.
866
    $output = $input;
867
    unset($output[0], $output[2]);
868

    
869
    $this->executeReq($input, $output, 's3', array('invert' => TRUE));
870
  }
871

    
872
}
873

    
874
/**
875
 * Tests for rewrite.inc
876
 */
877
class FeedsTamperRewriteTestCase extends FeedsTamperUnitTestCase {
878

    
879
  protected $plugin_id = 'rewrite';
880

    
881
  public static function getInfo() {
882
    return array(
883
      'name' => 'Plugins: Rewrite',
884
      'description' => 'Unit tests for "Rewrite" plugin.',
885
      'group' => 'Feeds Tamper',
886
    );
887
  }
888

    
889
  public function executeRew($input, $output, $source, $settings) {
890
    $result = new stdClass();
891
    $result->items = array($input);
892
    $this->callback($result, 0, $source, $result->items[0][$source], $settings);
893
    $this->assertEqual($result->items[0][$source], $output);
894
  }
895

    
896
  public function test() {
897
    $settings = array('text' => '[title] - [body]');
898
    $input = array(
899
      'title' => 'HI YA!',
900
      'body' => "I'm the coolest.",
901
      'combined' => 'Blah, blah, blah',
902
    );
903
    $this->executeRew($input, "HI YA! - I'm the coolest.", 'combined', $settings);
904
  }
905

    
906
}
907

    
908
/**
909
 * Tests for sprintf.inc
910
 */
911
class FeedsTamperSprintfTestCase extends FeedsTamperUnitTestCase {
912
  protected $plugin_id = 'sprintf';
913

    
914
  public static function getInfo() {
915
    return array(
916
      'name' => 'Plugins: Format string',
917
      'description' => 'Unit tests for the "sprintf" plugin.',
918
      'group' => 'Feeds Tamper',
919
    );
920
  }
921

    
922
  public function test() {
923
    $this->execute('abc0123def', 'abc0123def', array('format' => '%s'));
924
    $this->execute('0123', '00000123', array('format' => '%08d'));
925
    $this->execute('65', 'A', array('format' => '%c'));
926
  }
927

    
928
}
929

    
930
/**
931
 * Tests for strip_tags.inc
932
 */
933
class FeedsTamperStripTagsTestCase extends FeedsTamperUnitTestCase {
934

    
935
  protected $plugin_id = 'strip_tags';
936

    
937
  public static function getInfo() {
938
    return array(
939
      'name' => 'Plugins: Strip Tags',
940
      'description' => 'Unit tests for the "Strip Tags" plugin.',
941
      'group' => 'Feeds Tamper',
942
    );
943
  }
944

    
945
  public function test() {
946
    $this->execute('sdfsdfsdfsdf<b>sdfsdf</b>sdfsdf', 'sdfsdfsdfsdfsdfsdfsdfsdf', array('allowed_tags' => NULL));
947
    $this->execute('sdfsdfsdfsdf<b>sdfsdfsdfsdf', 'sdfsdfsdfsdfsdfsdfsdfsdf', array('allowed_tags' => NULL));
948
    $this->execute('sdfsdfsdfsdf<i>sdfsdf</i><b>sdfs</b>dfsdfsdf', 'sdfsdfsdfsdf<i>sdfsdf</i>sdfsdfsdfsdf', array('allowed_tags' => '<i>'));
949
  }
950

    
951
}
952

    
953
/**
954
 * Tests for str_pad.inc
955
 */
956
class FeedsTamperStrPadTestCase extends FeedsTamperUnitTestCase {
957

    
958
  protected $plugin_id = 'str_pad';
959

    
960
  public static function getInfo() {
961
    return array(
962
      'name' => 'Plugins: Pad a string',
963
      'description' => 'Unit tests for "Pad a string" plugin.',
964
      'group' => 'Feeds Tamper',
965
    );
966
  }
967

    
968
  public function test() {
969
    $settings = array(
970
      'pad_length' => '10',
971
      'pad_string' => '',
972
      'pad_type' => STR_PAD_RIGHT,
973
    );
974
    $this->execute('hi', 'hi        ', $settings);
975
    $settings['pad_type'] = STR_PAD_LEFT;
976
    $this->execute('hi', '        hi', $settings);
977
    $settings['pad_type'] = STR_PAD_RIGHT;
978
    $settings['pad_string'] = '0';
979
    $settings['pad_length'] = '5';
980
    // Can't use 1.0 since 1.0 == 1.000
981
    $this->execute('A.0', 'A.000', $settings);
982
  }
983

    
984
}
985

    
986
/**
987
 * Tests for strtotime.inc
988
 */
989
class FeedsTamperStrToTimeTestCase extends FeedsTamperUnitTestCase {
990

    
991
  protected $plugin_id = 'strtotime';
992

    
993
  public static function getInfo() {
994
    return array(
995
      'name' => 'Plugins: String to Unix Timestamp',
996
      'description' => 'Unit tests for the "String to Unix Timestamp" plugin.',
997
      'group' => 'Feeds Tamper',
998
    );
999
  }
1000

    
1001
  public function test() {
1002
    $this->execute('1986-05-09 04:00:00 GMT', 515995200);
1003
    $this->execute('May 9, 1986 04:00:00 GMT', 515995200);
1004
    $this->execute('Fri, 09 May 1986 04:00:00 GMT', 515995200);
1005
  }
1006

    
1007
}
1008

    
1009
/**
1010
 * Tests for timetodate.inc
1011
 */
1012
class FeedsTamperTimeToDateTestCase extends FeedsTamperUnitTestCase {
1013

    
1014
  protected $plugin_id = 'timetodate';
1015

    
1016
  public static function getInfo() {
1017
    return array(
1018
      'name' => 'Plugins: Unix timestamp to Date',
1019
      'description' => 'Unit tests for the "Unix timestamp to Date" plugin.',
1020
      'group' => 'Feeds Tamper',
1021
    );
1022
  }
1023

    
1024
  public function test() {
1025
    $settings = array(
1026
      'date_format' => "\I\\t'\s g \o'\c\l\o\c\k \J\i\m\.",
1027
    );
1028
    // Use mktime() so that test works in different timezones.
1029
    $this->execute(mktime(7), "It's 7 o'clock Jim.", $settings);
1030
  }
1031

    
1032
}
1033

    
1034
/**
1035
 * Tests for trim.inc
1036
 */
1037
class FeedsTamperTrimTestCase extends FeedsTamperUnitTestCase {
1038

    
1039
  protected $plugin_id = 'trim';
1040

    
1041
  public static function getInfo() {
1042
    return array(
1043
      'name' => 'Plugins: Trim',
1044
      'description' => 'Unit tests for "Trim" plugin.',
1045
      'group' => 'Feeds Tamper',
1046
    );
1047
  }
1048

    
1049
  public function test() {
1050
    $settings = array(
1051
      'side' => 'trim',
1052
    );
1053
    $this->execute('  asdfasf  ', 'asdfasf', $settings);
1054
    $settings['side'] = 'ltrim';
1055
    $this->execute('  asdfasf  ', 'asdfasf  ', $settings);
1056
    $settings['side'] = 'rtrim';
1057
    $this->execute('  asdfasf  ', '  asdfasf', $settings);
1058
    $settings['side'] = 'trim';
1059
    $settings['mask'] = '$';
1060
    $this->execute('$$asdfasf$$', 'asdfasf', $settings);
1061
  }
1062

    
1063
}
1064

    
1065
/**
1066
 * Tests for truncate_text.inc
1067
 */
1068
class FeedsTamperTruncateTextTestCase extends FeedsTamperUnitTestCase {
1069

    
1070
  protected $plugin_id = 'truncate_text';
1071

    
1072
  public static function getInfo() {
1073
    return array(
1074
      'name' => 'Plugins: Truncate Text',
1075
      'description' => 'Unit tests for "Truncate Text" plugin.',
1076
      'group' => 'Feeds Tamper',
1077
    );
1078
  }
1079

    
1080
  public function test() {
1081
    $this->execute('Hello, how are you today?', 'Hello', array('num_char' => 5, 'ellipses' => FALSE));
1082
    $this->execute('Hello, how are you today?', 'He...', array('num_char' => 5, 'ellipses' => TRUE));
1083
    $this->execute('Hello', 'Hello', array('num_char' => 5, 'ellipses' => TRUE));
1084
    $this->execute('Hello, how are you today?', 'Hello, how', array('num_char' => 12, 'ellipses' => FALSE, 'wordsafe' => TRUE));
1085
  }
1086

    
1087
}
1088

    
1089
/**
1090
 * Tests for unique.inc
1091
 */
1092
class FeedsTamperUniqueTestCase extends FeedsTamperUnitTestCase {
1093

    
1094
  protected $plugin_id = 'unique';
1095

    
1096
  public static function getInfo() {
1097
    return array(
1098
      'name' => 'Plugins: Unique',
1099
      'description' => 'Unit tests for "Unique" plugin.',
1100
      'group' => 'Feeds Tamper',
1101
    );
1102
  }
1103

    
1104
  public function test() {
1105
    $this->execute(array('a', 'a', 'b', 'c'), array('a', 'b', 'c'));
1106
    $this->execute(array(1, 1, 2, 3, 4), array(1, 2, 3, 4));
1107
  }
1108

    
1109
}
1110

    
1111
/**
1112
 * Tests for urlencode.inc
1113
 */
1114
class FeedsTamperURLencodeTestCase extends FeedsTamperUnitTestCase {
1115

    
1116
  protected $plugin_id = 'urlencode';
1117

    
1118
  public static function getInfo() {
1119
    return array(
1120
      'name' => 'Plugins: URLencode text',
1121
      'description' => 'Unit tests for "URL Encode" plugin.',
1122
      'group' => 'Feeds Tamper',
1123
    );
1124
  }
1125

    
1126
  public function test() {
1127
    $settings = array('method' => 'legacy');
1128
    $this->execute('$ & < > ? ; # : = , " \' ~ + %', '%24+%26+%3C+%3E+%3F+%3B+%23+%3A+%3D+%2C+%22+%27+%7E+%2B+%25', $settings);
1129
    $this->execute('String with spaces', 'String+with+spaces', $settings);
1130
    $this->execute('special chars: &%*', 'special+chars%3A+%26%25%2A', $settings);
1131

    
1132
    $settings = array('method' => 'raw');
1133
    $this->execute('$ & < > ? ; # : = , " \' ~ + %', '%24%20%26%20%3C%20%3E%20%3F%20%3B%20%23%20%3A%20%3D%20%2C%20%22%20%27%20~%20%2B%20%25', $settings);
1134
    $this->execute('String with spaces', 'String%20with%20spaces', $settings);
1135
    $this->execute('special chars: &%*', 'special%20chars%3A%20%26%25%2A', $settings);
1136
  }
1137

    
1138
}
1139

    
1140
/**
1141
 * Tests for urlencode.inc
1142
 */
1143
class FeedsTamperURLdecodeTestCase extends FeedsTamperUnitTestCase {
1144

    
1145
  protected $plugin_id = 'urldecode';
1146

    
1147
  public static function getInfo() {
1148
    return array(
1149
      'name' => 'Plugins: URLdecode text',
1150
      'description' => 'Unit tests for "URL Decode" plugin.',
1151
      'group' => 'Feeds Tamper',
1152
    );
1153
  }
1154

    
1155
  public function test() {
1156
    $settings = array('method' => 'legacy');
1157
    $this->execute('%24+%26+%3C+%3E+%3F+%3B+%23+%3A+%3D+%2C+%22+%27+%7E+%2B+%25', '$ & < > ? ; # : = , " \' ~ + %', $settings);
1158
    $this->execute('String+with+spaces', 'String with spaces', $settings);
1159
    $this->execute('special+chars%3A+%26%25%2A', 'special chars: &%*', $settings);
1160

    
1161
    $settings = array('method' => 'raw');
1162
    $this->execute('%24%20%26%20%3C%20%3E%20%3F%20%3B%20%23%20%3A%20%3D%20%2C%20%22%20%27%20%7E%20%2B%20%25', '$ & < > ? ; # : = , " \' ~ + %', $settings);
1163
    $this->execute('String%20with%20spaces', 'String with spaces', $settings);
1164
    $this->execute('special%20chars%3A%20%26%25%2A', 'special chars: &%*', $settings);
1165
  }
1166

    
1167
}
1168

    
1169
/**
1170
 * Tests for the Tamper plugin 'encode'.
1171
 */
1172
class FeedsTamperEncodeDecodeTestCase extends FeedsTamperUnitTestCase {
1173

    
1174
  /**
1175
   * The ID of the plugin to be tested.
1176
   *
1177
   * @var string
1178
   */
1179
  protected $plugin_id = 'encode';
1180

    
1181
  /**
1182
   * {@inheritdoc}
1183
   */
1184
  public static function getInfo() {
1185
    return array(
1186
      'name' => 'Plugins: Encode',
1187
      'description' => 'Unit tests for "Encode/Decode" plugin.',
1188
      'group' => 'Feeds Tamper',
1189
    );
1190
  }
1191

    
1192
  /**
1193
   * {@inheritdoc}
1194
   */
1195
  public function test() {
1196
    $this->execute(array(), 'a:0:{}', array('mode' => 'serialize'));
1197
    $this->execute('a:0:{}', array(), array('mode' => 'unserialize'));
1198

    
1199
    $this->execute('abcdef 123 @#`|\\"$%&/()=?\'^*', 's:28:"abcdef 123 @#`|\"$%&/()=?\'^*";', array('mode' => 'serialize'));
1200
    $this->execute('s:28:"abcdef 123 @#`|\"$%&/()=?\'^*";', 'abcdef 123 @#`|\"$%&/()=?\'^*', array('mode' => 'unserialize'));
1201

    
1202
    $this->execute('abcdef 123 @#`|\\"$%&/()=?\'^*', 'YWJjZGVmIDEyMyBAI2B8XCIkJSYvKCk9PydeKg==', array('mode' => 'base64_encode'));
1203
    $this->execute('YWJjZGVmIDEyMyBAI2B8XCIkJSYvKCk9PydeKg==', 'abcdef 123 @#`|\\"$%&/()=?\'^*', array('mode' => 'base64_decode'));
1204
  }
1205

    
1206
}