Skip to content

Commit

Permalink
tests: Add unit tests for issue #108
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Dickman committed Jan 23, 2020
1 parent 572f3c1 commit 5487f5c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
7 changes: 4 additions & 3 deletions classes/robot/crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -916,16 +916,17 @@ private function link_from_node_to_url($from, $url, $text, $idattr) {
global $DB;

// Ascertain the correct node level based on parent node level.
if ($from->level == TOOL_CRAWLER_NODE_LEVEL_PARENT) {
if (!empty($from->level) && $from->level == TOOL_CRAWLER_NODE_LEVEL_PARENT) {
$level = TOOL_CRAWLER_NODE_LEVEL_DIRECT_CHILD;
} else {
$level = TOOL_CRAWLER_NODE_LEVEL_INDIRECT_CHILD;
}

$priority = $from->priority ? $from->priority : TOOL_CRAWLER_PRIORITY_DEFAULT;
$priority = isset($from->priority) ? $from->priority : TOOL_CRAWLER_PRIORITY_DEFAULT;
$courseid = isset($from->courseid) ? $from->courseid : null;

// Add the node URL to the queue.
$to = $this->mark_for_crawl($from->url, $url, $from->courseid, $priority, $level);
$to = $this->mark_for_crawl($from->url, $url, $courseid, $priority, $level);
if ($to === false) {
return false;
}
Expand Down
66 changes: 64 additions & 2 deletions tests/phpunit/robot_crawler_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ public function test_should_be_excluded() {
$node->contents = $page . $linktoexclude;
$node->url = $url;
$node->id = $insertid;
$node->level = TOOL_CRAWLER_NODE_LEVEL_PARENT;

$this->resetAfterTest(true);

Expand All @@ -310,6 +311,67 @@ public function test_should_be_excluded() {
self::assertFalse($found);
}

/**
* Test for issue #108 - passing node crawl priority to child nodes when parsing html.
*/
public function test_parse_html_priority_inheritance() {
global $CFG, $DB;

$parentlocalurl = 'course/view.php?id=1&section=2';
$directchildlocalurl = 'mod/book/view.php?id=7';
$indirectchildexternalurl = 'http://someexternalsite.net.au';
$nodes = [];

// Internal parent node.
$node = $this->robot->mark_for_crawl($CFG->wwwroot, $parentlocalurl, 1, TOOL_CRAWLER_PRIORITY_HIGH);
$node->httpcode = 200;
$node->mimetype = 'text/html';
$node->external = 0;
$node->contents = <<<HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>Test title</title>
</head>
<body class="course-1">
<a href="$CFG->wwwroot/$directchildlocalurl">Direct child node</a>
</body>
</html>
HTML;
// Parse the parent node, to add create the direct child node.
$parentnode = $this->robot->parse_html($node, $node->external);

// Internal node direct child.
$url = new moodle_url('/' . $directchildlocalurl);
$node = $DB->get_record('tool_crawler_url', array('url' => $url->raw_out()) );
$node->url = $CFG->wwwroot.'/'.$directchildlocalurl;
$node->httpcode = 200;
$node->mimetype = 'text/html';
$node->external = 0;
$node->contents = <<<HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>Test title</title>
</head>
<body class="course-1">
<a href="$indirectchildexternalurl">Indirect child node</a>
</body>
</html>
HTML;
// Parse the direct child, to create the indirect child node.
$directchildnode = $this->robot->parse_html($node, $node->external);
$indirectchildnode = $DB->get_record('tool_crawler_url', ['url' => $indirectchildexternalurl]);

// Direct child nodes should inherit priority from parent node (super node).
$this->assertEquals($parentnode->priority, $directchildnode->priority);
// Indirect child nodes should not inherit a high priority from parent node (super node).
$this->assertGreaterThanOrEqual($indirectchildnode->priority, $parentnode->priority);
// Indirect child nodes should not inherit a high priority from parent node (super node).
$this->assertGreaterThanOrEqual($indirectchildnode->priority, $directchildnode->priority);
// Indirect child nodes should not be able to have a high priority.
$this->assertLessThan(TOOL_CRAWLER_PRIORITY_HIGH, $indirectchildnode->priority);
}
}


0 comments on commit 5487f5c

Please sign in to comment.