-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.php
More file actions
617 lines (513 loc) · 16.1 KB
/
test.php
File metadata and controls
617 lines (513 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
<?php
/**
* Tests for the Virginia Code scraper.
*
* Usage: php test.php
*
* Runs unit tests against the scraper's parsing and XML generation functions,
* then validates a sample of live CSV data produces well-formed XML.
*/
require __DIR__ . '/scraper.php';
$passed = 0;
$failed = 0;
function assert_equal($expected, $actual, string $test_name): void
{
global $passed, $failed;
if ($expected === $actual)
{
$passed++;
}
else
{
$failed++;
echo "FAIL: $test_name\n";
echo " Expected: " . var_export($expected, true) . "\n";
echo " Actual: " . var_export($actual, true) . "\n";
}
}
function assert_true(bool $condition, string $test_name): void
{
global $passed, $failed;
if ($condition)
{
$passed++;
}
else
{
$failed++;
echo "FAIL: $test_name\n";
}
}
/*
* =========================================================================
* extract_paragraphs()
* =========================================================================
*/
echo "Testing extract_paragraphs...\n";
assert_equal(
[],
extract_paragraphs(''),
'empty string returns empty array'
);
assert_equal(
['Hello world.'],
extract_paragraphs('<p>Hello world.</p>'),
'single paragraph'
);
assert_equal(
['First.', 'Second.'],
extract_paragraphs('<p>First.</p><p>Second.</p>'),
'two paragraphs'
);
assert_equal(
['Link to 18.2-31 here.'],
extract_paragraphs('<p>Link to <a href="/vacode/18.2-31/">18.2-31</a> here.</p>'),
'HTML tags are stripped'
);
assert_equal(
['"Quoted" text & more.'],
extract_paragraphs('<p>"Quoted" text & more.</p>'),
'HTML entities are decoded'
);
assert_equal(
['Content here.'],
extract_paragraphs('<p class=\'sidenote\'>Content here.</p>'),
'paragraphs with attributes are matched'
);
/*
* =========================================================================
* extract_history()
* =========================================================================
*/
echo "Testing extract_history...\n";
assert_equal(
'',
extract_history([]),
'empty array returns empty string'
);
assert_equal(
'1975, cc. 14, 15.',
extract_history(['Some law text.', '1975, cc. 14, 15.']),
'year-starting history is extracted'
);
assert_equal(
'Code 1919, § 2, § 1-10; 2005, c. 839.',
extract_history(['Some text.', 'Code 1919, § 2, § 1-10; 2005, c. 839.']),
'Code-starting history is extracted'
);
assert_equal(
'R. P. 1948, § 1-2.1.',
extract_history(['Some text.', 'R. P. 1948, § 1-2.1.']),
'R. P.-starting history is extracted'
);
assert_equal(
'Acts 1966, c. 702.',
extract_history(['Repealed by Acts 1966, c. 702.', 'Acts 1966, c. 702.']),
'Acts-starting history is extracted'
);
assert_equal(
'',
extract_history(['This is not history text.']),
'non-history paragraph returns empty string'
);
assert_equal(
'',
extract_history(['A. Some subsection text that starts with a letter.']),
'subsection text is not mistaken for history'
);
/*
* =========================================================================
* detect_prefix()
* =========================================================================
*/
echo "Testing detect_prefix...\n";
assert_equal(
'A. ',
detect_prefix('A. Some text here.'),
'uppercase letter prefix'
);
assert_equal(
'Z. ',
detect_prefix('Z. Last letter.'),
'letter Z prefix'
);
assert_equal(
'A1. ',
detect_prefix('A1. Subsection A1 text.'),
'letter+digit prefix'
);
assert_equal(
'C2. ',
detect_prefix('C2. Another subsection.'),
'C2 prefix'
);
assert_equal(
'1. ',
detect_prefix('1. Numbered item.'),
'numbered prefix'
);
assert_equal(
'12. ',
detect_prefix('12. Multi-digit number.'),
'multi-digit numbered prefix'
);
assert_equal(
'(a) ',
detect_prefix('(a) Parenthesized lowercase.'),
'parenthesized lowercase letter'
);
assert_equal(
'(1) ',
detect_prefix('(1) Parenthesized number.'),
'parenthesized number'
);
assert_equal(
'(ii) ',
detect_prefix('(ii) Roman numeral.'),
'parenthesized roman numeral'
);
assert_equal(
'(A) ',
detect_prefix('(A) Parenthesized uppercase.'),
'parenthesized uppercase letter'
);
assert_equal(
null,
detect_prefix('The common law of England shall continue.'),
'ordinary sentence returns null'
);
assert_equal(
null,
detect_prefix('"Person" includes any individual.'),
'quoted text returns null'
);
/*
* =========================================================================
* to_title_case()
* =========================================================================
*/
echo "Testing to_title_case...\n";
assert_equal(
'General Provisions',
to_title_case('GENERAL PROVISIONS'),
'all-caps converted to title case'
);
assert_equal(
'Code of Virginia',
to_title_case('CODE OF VIRGINIA'),
'minor word "of" stays lowercase'
);
assert_equal(
'Common Law and Rules of Construction',
to_title_case('COMMON LAW AND RULES OF CONSTRUCTION'),
'minor words "and", "of" stay lowercase'
);
assert_equal(
'Common Law and Rules of Construction',
to_title_case('Common Law and Rules of Construction'),
'already mixed case is left alone'
);
assert_equal(
'Citizenship [Repealed]',
to_title_case('CITIZENSHIP [Repealed]'),
'bracketed annotation preserved in title case'
);
assert_equal(
'Common Law, Statutes and Rules of Construction [Repealed]',
to_title_case('COMMON LAW, STATUTES AND RULES OF CONSTRUCTION [Repealed]'),
'all-caps with bracket converts correctly'
);
assert_equal(
'Jurisdiction Over Lands Acquired by the United States',
to_title_case('JURISDICTION OVER LANDS ACQUIRED BY THE UNITED STATES'),
'"by" and "the" stay lowercase mid-sentence'
);
assert_equal(
'In General',
to_title_case('In General'),
'mixed case left alone (In General)'
);
/*
* =========================================================================
* build_structure_xml()
* =========================================================================
*/
echo "Testing build_structure_xml...\n";
$row_simple = [
'TitleNum' => '1', 'TitleName' => 'General Provisions',
'SubTitleNum' => '', 'SubTitleName' => '',
'PartNum' => '', 'PartName' => '',
'ChapterNum' => '2.1', 'ChapterName' => 'Common Law',
'SubPartNum' => '', 'SubPartName' => '',
'ArticleNum' => '1', 'ArticleName' => 'Common Law and Acts',
];
$structure = build_structure_xml($row_simple);
assert_true(
strpos($structure, 'label="title" identifier="1" level="1"') !== false,
'title unit is present at level 1'
);
assert_true(
strpos($structure, 'label="chapter" identifier="2.1" level="2"') !== false,
'chapter unit is at level 2 (skipping empty subtitle/part)'
);
assert_true(
strpos($structure, 'label="article" identifier="1" level="3"') !== false,
'article unit is at level 3'
);
assert_true(
strpos($structure, 'label="subtitle"') === false,
'empty subtitle is omitted'
);
$row_full = [
'TitleNum' => '2.2', 'TitleName' => 'Administration of Government',
'SubTitleNum' => 'I', 'SubTitleName' => 'Organization',
'PartNum' => 'A', 'PartName' => 'Office of Governor',
'ChapterNum' => '1', 'ChapterName' => 'Governor',
'SubPartNum' => '', 'SubPartName' => '',
'ArticleNum' => '1', 'ArticleName' => 'General Provisions',
];
$structure_full = build_structure_xml($row_full);
assert_true(
strpos($structure_full, 'label="subtitle" identifier="I" level="2"') !== false,
'subtitle is at level 2 when present'
);
assert_true(
strpos($structure_full, 'label="part" identifier="A" level="3"') !== false,
'part is at level 3 when present'
);
assert_true(
strpos($structure_full, 'label="chapter" identifier="1" level="4"') !== false,
'chapter is at level 4 with subtitle+part before it'
);
assert_true(
strpos($structure_full, 'label="article" identifier="1" level="5"') !== false,
'article is at level 5'
);
/*
* =========================================================================
* build_text_xml()
* =========================================================================
*/
echo "Testing build_text_xml...\n";
assert_equal('', build_text_xml([]), 'empty paragraphs returns empty string');
$text = build_text_xml(['The common law shall continue.']);
assert_true(
strpos($text, 'The common law shall continue.') !== false,
'plain paragraph is included as text'
);
assert_true(
strpos($text, '<section') === false,
'plain paragraph has no section wrapper'
);
$text_sub = build_text_xml(['A. First subsection.', 'B. Second subsection.']);
assert_true(
strpos($text_sub, '<section prefix="A">First subsection.</section>') !== false,
'A. prefix is parsed into section element'
);
assert_true(
strpos($text_sub, '<section prefix="B">Second subsection.</section>') !== false,
'B. prefix is parsed into section element'
);
$text_paren = build_text_xml(['(a) Lowercase paren.', '(1) Numbered paren.']);
assert_true(
strpos($text_paren, '<section prefix="a">Lowercase paren.</section>') !== false,
'(a) prefix is parsed, parens stripped'
);
assert_true(
strpos($text_paren, '<section prefix="1">Numbered paren.</section>') !== false,
'(1) prefix is parsed, parens stripped'
);
/*
* =========================================================================
* generate_xml() — full integration
* =========================================================================
*/
echo "Testing generate_xml...\n";
$row = [
'TitleNum' => '18.2', 'TitleName' => 'Crimes and Offenses',
'SubTitleNum' => '', 'SubTitleName' => '',
'PartNum' => '', 'PartName' => '',
'ChapterNum' => '1', 'ChapterName' => 'In General',
'SubPartNum' => '', 'SubPartName' => '',
'ArticleNum' => '1', 'ArticleName' => 'Transition',
'Section' => '18.2-1',
'Title' => 'Repealing clause',
'Body' => '<p>A. First provision.</p><p>B. Second provision.</p><p>1975, cc. 14, 15.</p>',
];
$xml = generate_xml($row);
assert_true(
strpos($xml, '<?xml version="1.0" encoding="utf-8"?>') === 0,
'XML declaration is present'
);
assert_true(
strpos($xml, '<law>') !== false,
'root <law> element is present'
);
assert_true(
strpos($xml, '<section_number>18.2-1</section_number>') !== false,
'section_number is correct'
);
assert_true(
strpos($xml, '<catch_line>Repealing clause</catch_line>') !== false,
'catch_line is correct'
);
assert_true(
strpos($xml, '<section prefix="A">First provision.</section>') !== false,
'subsection A is in text'
);
assert_true(
strpos($xml, '<section prefix="B">Second provision.</section>') !== false,
'subsection B is in text'
);
assert_true(
strpos($xml, '<history>1975, cc. 14, 15.</history>') !== false,
'history is extracted and separated from text'
);
assert_true(
strpos($xml, '>1975, cc. 14, 15.<') === false
|| strpos($xml, '<history>1975') !== false,
'history does not appear inside <text>'
);
/*
* Validate that the generated XML is well-formed.
*/
$doc = new DOMDocument();
$well_formed = @$doc->loadXML($xml);
assert_true($well_formed !== false, 'generated XML is well-formed');
/*
* =========================================================================
* XML special characters
* =========================================================================
*/
echo "Testing XML special character escaping...\n";
$row_special = [
'TitleNum' => '1', 'TitleName' => 'Provisions & Rules',
'SubTitleNum' => '', 'SubTitleName' => '',
'PartNum' => '', 'PartName' => '',
'ChapterNum' => '1', 'ChapterName' => 'The "Code"',
'SubPartNum' => '', 'SubPartName' => '',
'ArticleNum' => '', 'ArticleName' => '',
'Section' => '1-1',
'Title' => 'Contents & designation of "Code"',
'Body' => '<p>Laws in "this Code" & following titles.</p><p>Code 1919, § 1.</p>',
];
$xml_special = generate_xml($row_special);
$doc_special = new DOMDocument();
$well_formed_special = @$doc_special->loadXML($xml_special);
assert_true($well_formed_special !== false, 'XML with special characters is well-formed');
assert_true(
strpos($xml_special, 'Provisions & Rules') !== false,
'ampersand in title name is escaped'
);
assert_true(
strpos($xml_special, 'Contents & designation') !== false,
'ampersand in catch_line is escaped'
);
/*
* =========================================================================
* parse_csv()
* =========================================================================
*/
echo "Testing parse_csv...\n";
$csv_input = "TitleNum,Section,Title,Body\n"
. "1,1-1,Contents,\"<p>Some text.</p>\"\n"
. "1,1-2,Effective date,\"<p>More text.</p>\"\n";
$parsed = parse_csv($csv_input);
assert_equal(2, count($parsed), 'parse_csv returns correct row count');
assert_equal('1-1', $parsed[0]['Section'], 'first row Section is correct');
assert_equal('1-2', $parsed[1]['Section'], 'second row Section is correct');
assert_equal('<p>Some text.</p>', $parsed[0]['Body'], 'quoted HTML body is preserved');
/*
* =========================================================================
* Edge case: repealed section with no history
* =========================================================================
*/
echo "Testing edge cases...\n";
$row_repealed = [
'TitleNum' => '1', 'TitleName' => 'General Provisions',
'SubTitleNum' => '', 'SubTitleName' => '',
'PartNum' => '', 'PartName' => '',
'ChapterNum' => '2', 'ChapterName' => 'Repealed Chapter',
'SubPartNum' => '', 'SubPartName' => '',
'ArticleNum' => '', 'ArticleName' => '',
'Section' => '1-10',
'Title' => 'Repealed',
'Body' => '<p>Repealed by Acts 2005, c. 839, cl. 10.</p>',
];
$xml_repealed = generate_xml($row_repealed);
$doc_repealed = new DOMDocument();
assert_true(
@$doc_repealed->loadXML($xml_repealed) !== false,
'repealed section XML is well-formed'
);
assert_true(
strpos($xml_repealed, '<history></history>') !== false,
'repealed section has empty history (text is not a history pattern)'
);
/*
* Edge case: section with empty body.
*/
$row_empty = [
'TitleNum' => '1', 'TitleName' => 'General Provisions',
'SubTitleNum' => '', 'SubTitleName' => '',
'PartNum' => '', 'PartName' => '',
'ChapterNum' => '1', 'ChapterName' => 'Code',
'SubPartNum' => '', 'SubPartName' => '',
'ArticleNum' => '', 'ArticleName' => '',
'Section' => '1-0',
'Title' => 'Empty section',
'Body' => '',
];
$xml_empty = generate_xml($row_empty);
$doc_empty = new DOMDocument();
assert_true(
@$doc_empty->loadXML($xml_empty) !== false,
'empty body section XML is well-formed'
);
/*
* =========================================================================
* Live validation: download a title and validate all generated XML
* =========================================================================
*/
echo "Testing live CSV download and XML validation...\n";
$csv_url = 'https://law.lis.virginia.gov/CSV/CoVTitle_1.csv';
$csv_data = @file_get_contents($csv_url);
if ($csv_data === false)
{
echo " SKIP: Could not download live CSV (network unavailable)\n";
}
else
{
$rows = parse_csv($csv_data);
assert_true(count($rows) > 0, 'live CSV has rows');
$xml_errors = 0;
$xml_count = 0;
foreach ($rows as $row)
{
if (empty($row['Section']))
{
continue;
}
$xml = generate_xml($row);
$xml_count++;
$doc = new DOMDocument();
if (@$doc->loadXML($xml) === false)
{
$xml_errors++;
if ($xml_errors <= 3)
{
echo " INVALID XML for section " . $row['Section'] . "\n";
}
}
}
assert_equal(0, $xml_errors, "all $xml_count live sections produce well-formed XML");
echo " Validated $xml_count sections from Title 1\n";
}
/*
* =========================================================================
* Results
* =========================================================================
*/
echo "\n";
echo "Results: $passed passed, $failed failed\n";
exit($failed > 0 ? 1 : 0);