diff --git a/iqInternalTest.php b/iqInternalTest.php new file mode 100644 index 0000000..3e8ca73 --- /dev/null +++ b/iqInternalTest.php @@ -0,0 +1,137 @@ +assertFileExists($uri); + $data = file_get_contents($uri); + $this->assertFalse(empty($data), "Data is read"); + $json = json_decode($data, TRUE); + + $prepared = _drush_iq_parse_json($data); + $this->assertFalse(empty($prepared), "Processed into our dataset"); + } + + public function testDownloadIssueData() { + $nid = "2125253"; + $node = $this->downloadNode($nid); + $author = $node->author; + $this->addUser($node, $author->id); + + $comments = $node->comments; + foreach ($comments as $comment) { + // We could use $comment->uri if using Accept header and getting response + $node->_drush_data["comment/" . $comment->id] = $this->downloadComment($comment->id); + $this->addUser($node, $node->_drush_data["comment/" . $comment->id]->author->id); + } + + + foreach ($node->field_issue_files as $issue_file) { + // $issue_file is a compound object of display + file + $uri = 'file/' . $issue_file->file->id; + $node->_drush_data[$uri] = $this->fetchJson($uri); + $this->addUser($node, $node->_drush_data[$uri]->owner->id); + } + print_r($node); + } + + function addUser($item, $id) { + $uri = 'user/' . $id; + $item->_drush_data[$uri] = $this->fetchJson($uri); + } + + function downloadNode($id) { + return $this->fetchJson("node/$id"); + } + + function downloadComment($id) { + return $this->fetchJson("comment/$id"); + } + + function downloadUser($id) { + return $this->fetchJson("user/$id"); + } + + function fetchJson($uri) { + if (!$content = $this->cache_get($uri)) { + $content = $this->fetchUri($uri); + } + $this->assertJson($content, "Fetched json from $uri"); + $json = json_decode($content); + file_put_contents(__DIR__ . '/tests/data/download/' . $uri . '.json', json_encode($json, JSON_PRETTY_PRINT)); + return json_decode($content); + } + + function cache_get($uri) { + $path = __DIR__ . '/tests'; + @mkdir($path); + @mkdir($path . '/data'); + @mkdir($path . '/data/download'); + @mkdir($path . '/data/download/node'); + @mkdir($path . '/data/download/comment'); + @mkdir($path . '/data/download/user'); + @mkdir($path . '/data/download/file'); + + $file = $path . '/data/download/' . $uri . '.json'; + // this fails on my system: why? + // mkdir(dirname($file), 0777, TRUE); + if (file_exists($file)) { + return file_get_contents($file); + } + } + + function fetchUri($uri) { + $url = $this->buildJsonUri($uri); + $options = array( + 'http' => array( + 'method' => "GET", + 'header' => "Accept-language: en\r\nAccept: application/json\r\n" . + "Cookie: foo=bar\r\n" . // check function.stream-context-create on php.net + "User-Agent: Drush\r\n" + ) + ); + //drush_download_file($url); + $context = stream_context_create($options); + $file = file_get_contents($url, false, $context); + return $file; + } + + function buildJsonUri($uri) { + //return $this->site . '/' . $uri . '.json'; + return $this->site . '/' . $uri; + } + +}