Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
StringTest.php
1 <?php
2 
18 require_mock_env('env1');
19 
20 import('lib.pkp.tests.PKPTestCase');
21 import('lib.pkp.classes.core.String');
22 
23 class StringTest extends PKPTestCase {
27  public function testTitleCase() {
28  AppLocale::setTranslations(array('common.titleSmallWords' => 'is a'));
29  $originalTitle = 'AND This IS A TEST title';
30  self::assertEquals('And This is a Test Title', String::titleCase($originalTitle));
31  }
32 
36  public function testTrimPunctuation() {
37  $trimmedChars = array(
38  ' ', ',', '.', ';', ':', '!', '?',
39  '(', ')', '[', ']', '\\', '/'
40  );
41 
42  foreach($trimmedChars as $trimmedChar) {
43  self::assertEquals('trim.med',
44  String::trimPunctuation($trimmedChar.'trim.med'.$trimmedChar));
45  }
46  }
47 
51  public function testDiff() {
52  // Test two strings that have common substrings.
53  $originalString = 'The original string.';
54  $editedString = 'The edited original.';
55  $expectedDiff = array(
56  array( 0 => 'The'),
57  array( 1 => ' edited'),
58  array( 0 => ' original'),
59  array( -1 => ' string'),
60  array( 0 => '.')
61  );
62  $resultDiff = String::diff($originalString, $editedString);
63  self::assertEquals($expectedDiff, $resultDiff);
64 
65  // Test two completely different strings.
66  $originalString = 'abc';
67  $editedString = 'def';
68  $expectedDiff = array(
69  array( -1 => 'abc'),
70  array( 1 => 'def')
71  );
72  $resultDiff = String::diff($originalString, $editedString);
73  self::assertEquals($expectedDiff, $resultDiff);
74 
75  // A more realistic example from the citation editor use case
76  $originalString = 'Willinsky, B. (2006). The access principle: The case for open acces to research and scholarship. Cambridge, MA: MIT Press.';
77  $editedString = 'Willinsky, J. (2006). The access principle: The case for open access to research and scholarship. Cambridge, MA: MIT Press.';
78  $expectedDiff = array(
79  array( 0 => 'Willinsky, ' ),
80  array( -1 => 'B' ),
81  array( 1 => 'J' ),
82  array( 0 => '. (2006). The access principle: The case for open acce' ),
83  array( 1 => 's' ),
84  array( 0 => 's to research and scholarship. Cambridge, MA: MIT Press.' )
85  );
86  $resultDiff = String::diff($originalString, $editedString);
87  self::assertEquals($expectedDiff, $resultDiff);
88  }
89 }
90 ?>
static trimPunctuation($string)
Definition: String.inc.php:685
static setTranslations($translations)
testTrimPunctuation()
Definition: StringTest.php:36
static titleCase($title)
Definition: String.inc.php:694
Tests for the String class.
Definition: StringTest.php:23
static diff($originalString, $editedString)
Definition: String.inc.php:824
Class that implements functionality common to all PKP unit test cases.