Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletest/testsimpletestlib.php
Go to the documentation of this file.
00001 <?php
00010 if (!defined('MOODLE_INTERNAL')) {
00011     die('Direct access to this script is forbidden.');    
00012 }
00013 
00014 class simpletestlib_test extends UnitTestCaseUsingDatabase {
00015 
00016     function test_table_creation_and_data() {
00017         global $DB;
00018 
00019         $this->switch_to_test_db(); // All operations until end of test method will happen in test DB
00020         $dbman = $DB->get_manager();
00021 
00022         // Create table and test
00023         $this->create_test_table('context', 'lib');
00024         $this->assertTrue($dbman->table_exists('context'));
00025 
00026         $sampledata = array(
00027                           array('contextlevel' => 10, 'instanceid' => 666, 'path' => '', 'depth' => 1),
00028                           array('contextlevel' => 40, 'instanceid' => 666, 'path' => '', 'depth' => 2),
00029                           array('contextlevel' => 50, 'instanceid' => 666, 'path' => '', 'depth' => 3));
00030 
00031         foreach($sampledata as $key => $record) {
00032             $sampledata[$key]['id'] = $DB->insert_record('context', $record);
00033         }
00034 
00035         // Just test added data and delete later
00036         $this->assertEqual($DB->count_records('context'), 3);
00037         $this->assertTrue($DB->record_exists('context', array('id' => $sampledata[0]['id'])));
00038         $this->assertTrue($DB->get_field('context', 'contextlevel', array('id' => $sampledata[2]['id'])), $sampledata[2]['contextlevel']);
00039         $DB->delete_records('context');
00040         $this->assertFalse($DB->record_exists('context', array('id' => $sampledata[1]['id'])));
00041     }
00042 
00043     function test_tables_are_dropped() {
00044         global $DB;
00045 
00046         $this->switch_to_test_db(); // All operations until end of test method will happen in test DB
00047         $dbman = $DB->get_manager();
00048         // Previous method tearDown *must* delete all created tables, so here 'context' must not exist anymore
00049         $this->assertFalse($dbman->table_exists('context'));
00050     }
00051 }
00052 
00053 
00060 class ContainsTagWithAttribute_test extends UnitTestCase {
00061     function test_simple() {
00062         $expectation = new ContainsTagWithAttribute('span', 'class', 'error');
00063         $this->assertTrue($expectation->test('<span class="error">message</span>'));
00064     }
00065 
00066     function test_other_attrs() {
00067         $expectation = new ContainsTagWithAttribute('span', 'class', 'error');
00068         $this->assertTrue($expectation->test('<span     oneattr="thingy"   class  =  "error"  otherattr="thingy">message</span>'));
00069     }
00070 
00071     function test_fails() {
00072         $expectation = new ContainsTagWithAttribute('span', 'class', 'error');
00073         $this->assertFalse($expectation->test('<span class="mismatch">message</span>'));
00074     }
00075 
00076     function test_link() {
00077         $html = '<a href="http://www.test.com">Click Here</a>';
00078         $expectation = new ContainsTagWithAttribute('a', 'href', 'http://www.test.com');
00079         $this->assertTrue($expectation->test($html));
00080         $this->assert(new ContainsTagWithContents('a', 'Click Here'), $html);
00081     }
00082 
00083     function test_garbage() {
00084         $expectation = new ContainsTagWithAttribute('a', 'href', '!#@*%@_-)(*#0-735\\fdf//fdfg235-0970}$@}{#:~');
00085         $this->assertTrue($expectation->test('<a href="!#@*%@_-)(*#0-735\\fdf//fdfg235-0970}$@}{#:~">Click Here</a>'));
00086 
00087     }
00088 
00089     function test_inline_js() {
00090         $html = '<a title="Popup window" href="http://otheraddress.com" class="link" onclick="this.target=\'my_popup\';">Click here</a>';
00091         $this->assert(new ContainsTagWithAttribute('a', 'href', 'http://otheraddress.com'), $html);
00092     }
00093 
00094     function test_real_regression1() {
00095         $expectation = new ContainsTagWithAttribute('label', 'for', 'html_select4ac387224bf9d');
00096         $html = '<label for="html_select4ac387224bf9d">Cool menu</label><select name="mymenu" id="html_select4ac387224bf9d" class="menumymenu select"> <option value="0">Choose...</option><option value="10">ten</option><option value="c2">two</option></select>';
00097         $this->assert($expectation, $html);
00098     }
00099 
00100     function test_zero_attr() {
00101         $expectation = new ContainsTagWithAttribute('span', 'class', 0);
00102         $this->assertTrue($expectation->test('<span class="0">message</span>'));
00103     }
00104 
00105     function test_zero_attr_does_not_match_blank() {
00106         $expectation = new ContainsTagWithAttribute('span', 'class', 0);
00107         $this->assertFalse($expectation->test('<span class="">message</span>'));
00108     }
00109 
00110     function test_blank_attr() {
00111         $expectation = new ContainsTagWithAttribute('span', 'class', '');
00112         $this->assertTrue($expectation->test('<span class="">message</span>'));
00113     }
00114 
00115     function test_blank_attr_does_not_match_zero() {
00116         $expectation = new ContainsTagWithAttribute('span', 'class', '');
00117         $this->assertFalse($expectation->test('<span class="0">message</span>'));
00118     }
00119 }
00120 
00121 
00128 class ContainsTagWithAttributes_test extends UnitTestCase {
00129     function test_simple() {
00130         $content = <<<END
00131 <input id="qIhr6wWLTt3,1_omact_gen_14" name="qIhr6wWLTt3,1_omact_gen_14" onclick="if(this.hasSubmitted) { return false; } this.hasSubmitted=true; preSubmit(this.form); return true;" type="submit" value="Check" />
00132 END;
00133         $expectation = new ContainsTagWithAttributes('input',
00134                 array('type' => 'submit', 'name' => 'qIhr6wWLTt3,1_omact_gen_14', 'value' => 'Check'));
00135         $this->assert($expectation, $content);
00136     }
00137 
00138     function test_zero_attr() {
00139         $expectation = new ContainsTagWithAttributes('span', array('class' => 0));
00140         $this->assertTrue($expectation->test('<span class="0">message</span>'));
00141     }
00142 
00143     function test_zero_attr_does_not_match_blank() {
00144         $expectation = new ContainsTagWithAttributes('span', array('class' => 0));
00145         $this->assertFalse($expectation->test('<span class="">message</span>'));
00146     }
00147 
00148     function test_blank_attr() {
00149         $expectation = new ContainsTagWithAttributes('span', array('class' => ''));
00150         $this->assertTrue($expectation->test('<span class="">message</span>'));
00151     }
00152 
00153     function test_blank_attr_does_not_match_zero() {
00154         $expectation = new ContainsTagWithAttributes('span', array('class' => ''));
00155         $this->assertFalse($expectation->test('<span class="0">message</span>'));
00156     }
00157 }
00158 
00159 
00166 class ContainsTagWithContents_test extends UnitTestCase {
00167     function test_simple() {
00168         $expectation = new ContainsTagWithContents('span', 'message');
00169         $this->assertTrue($expectation->test('<span class="error">message</span>'));
00170     }
00171 
00172     function test_no_end() {
00173         $expectation = new ContainsTagWithContents('span', 'message');
00174         $this->assertFalse($expectation->test('<span class="error">message'));
00175     }
00176 }
00177 
00178 
00185 class ContainsSelectExpectation_test extends UnitTestCase {
00186     function test_matching_select_passes() {
00187         $expectation = new ContainsSelectExpectation('selectname', array('Choice1', 'Choice2'));
00188         $this->assertTrue($expectation->test('
00189                 <select name="selectname">
00190                     <option value="0">Choice1</option>
00191                     <option value="1">Choice2</option>
00192                 </select>'));
00193     }
00194 
00195     function test_fails_if_no_select() {
00196         $expectation = new ContainsSelectExpectation('selectname', array('Choice1', 'Choice2'));
00197         $this->assertFalse($expectation->test('<span>should not match</span>'));
00198     }
00199 
00200     function test_select_with_missing_choices_fails() {
00201         $expectation = new ContainsSelectExpectation('selectname', array('Choice1', 'Choice2'));
00202         $this->assertFalse($expectation->test('
00203                 <select name="selectname">
00204                     <option value="0">Choice1</option>
00205                 </select>'));
00206     }
00207 
00208     function test_select_with_extra_choices_fails() {
00209         $expectation = new ContainsSelectExpectation('selectname', array('Choice1'));
00210         $this->assertFalse($expectation->test('
00211                 <select name="selectname">
00212                     <option value="0">Choice1</option>
00213                     <option value="1">Choice2</option>
00214                 </select>'));
00215     }
00216 
00217     function test_select_with_wrong_order_choices_fails() {
00218         $expectation = new ContainsSelectExpectation('selectname', array('Choice1'));
00219         $this->assertFalse($expectation->test('
00220                 <select name="selectname">
00221                     <option value="1">Choice2</option>
00222                     <option value="0">Choice1</option>
00223                 </select>'));
00224     }
00225 
00226     function test_select_check_selected_pass() {
00227         $expectation = new ContainsSelectExpectation('selectname',
00228                 array('key1' => 'Choice1', 'key2' => 'Choice2'), 'key2');
00229         $this->assertTrue($expectation->test('
00230                 <select name="selectname">
00231                     <option value="key1">Choice1</option>
00232                     <option value="key2" selected="selected">Choice2</option>
00233                 </select>'));
00234     }
00235 
00236     function test_select_check_wrong_one_selected_fail() {
00237         $expectation = new ContainsSelectExpectation('selectname',
00238                 array('key1' => 'Choice1', 'key2' => 'Choice2'), 'key2');
00239         $this->assertFalse($expectation->test('
00240                 <select name="selectname">
00241                     <option value="key1" selected="selected">Choice1</option>
00242                     <option value="key2">Choice2</option>
00243                 </select>'));
00244     }
00245 
00246     function test_select_check_nothing_selected_fail() {
00247         $expectation = new ContainsSelectExpectation('selectname',
00248                 array('key1' => 'Choice1', 'key2' => 'Choice2'), 'key2');
00249         $this->assertFalse($expectation->test('
00250                 <select name="selectname">
00251                     <option value="key1">Choice1</option>
00252                     <option value="key2">Choice2</option>
00253                 </select>'));
00254     }
00255 
00256     function test_select_disabled_pass() {
00257         $expectation = new ContainsSelectExpectation('selectname',
00258                 array('key1' => 'Choice1', 'key2' => 'Choice2'), null, false);
00259         $this->assertTrue($expectation->test('
00260                 <select name="selectname" disabled="disabled">
00261                     <option value="key1">Choice1</option>
00262                     <option value="key2">Choice2</option>
00263                 </select>'));
00264     }
00265 
00266     function test_select_disabled_fail1() {
00267         $expectation = new ContainsSelectExpectation('selectname',
00268                 array('key1' => 'Choice1', 'key2' => 'Choice2'), null, true);
00269         $this->assertFalse($expectation->test('
00270                 <select name="selectname" disabled="disabled">
00271                     <option value="key1">Choice1</option>
00272                     <option value="key2">Choice2</option>
00273                 </select>'));
00274     }
00275 
00276     function test_select_disabled_fail2() {
00277         $expectation = new ContainsSelectExpectation('selectname',
00278                 array('key1' => 'Choice1', 'key2' => 'Choice2'), null, false);
00279         $this->assertFalse($expectation->test('
00280                 <select name="selectname">
00281                     <option value="key1">Choice1</option>
00282                     <option value="key2">Choice2</option>
00283                 </select>'));
00284     }
00285 }
00286 
00287 
00294 class DoesNotContainTagWithAttributes_test extends UnitTestCase {
00295     function test_simple() {
00296         $content = <<<END
00297 <input id="qIhr6wWLTt3,1_omact_gen_14" name="qIhr6wWLTt3,1_omact_gen_14" onclick="if(this.hasSubmitted) { return false; } this.hasSubmitted=true; preSubmit(this.form); return true;" type="submit" value="Check" />
00298 END;
00299         $expectation = new DoesNotContainTagWithAttributes('input',
00300                 array('type' => 'submit', 'name' => 'qIhr6wWLTt3,1_omact_gen_14', 'value' => 'Check'));
00301         $this->assertFalse($expectation->test($content));
00302     }
00303 
00304     function test_zero_attr() {
00305         $expectation = new DoesNotContainTagWithAttributes('span', array('class' => 0));
00306         $this->assertFalse($expectation->test('<span class="0">message</span>'));
00307     }
00308 
00309     function test_zero_different_attr_ok() {
00310         $expectation = new DoesNotContainTagWithAttributes('span', array('class' => 'shrub'));
00311         $this->assertTrue($expectation->test('<span class="tree">message</span>'));
00312     }
00313 
00314     function test_blank_attr() {
00315         $expectation = new DoesNotContainTagWithAttributes('span', array('class' => ''));
00316         $this->assertFalse($expectation->test('<span class="">message</span>'));
00317     }
00318 
00319     function test_blank_attr_does_not_match_zero() {
00320         $expectation = new ContainsTagWithAttributes('span', array('class' => ''));
00321         $this->assertFalse($expectation->test('<span class="0">message</span>'));
00322     }
00323 }
 All Data Structures Namespaces Files Functions Variables Enumerations