<?php

/**
 * @author fx
 */
class AdvancedServicesTest extends PHPUnit_Framework_TestCase
{

	public function setUp() {
		$this->servicesUrl = BASE_URL.'/services/';
		$this->outputFile = '/tmp/curl_output';
	}

	public function testBasicGetUrl()
	{
		$result = $this->loadService('dumpGetParameters.php?foo=bar');
		$this->assertContains('[foo] => bar', $result);
	}

	public function testGetImage() {
		$this->loadService('getImage.php?source=../test/resources/example.jpg', true);
		$this->assertEquals(filesize(__DIR__.'/resources/example.jpg'), filesize($this->outputFile));
	}

	public function testGetImageResizedDown() {
		$this->loadService('getImage.php?source=../test/resources/example.jpg&action=resize&width=100&height=100', true);
		$this->assertLessThan(filesize(__DIR__.'/resources/example.jpg'), filesize($this->outputFile));
		$size = getimagesize($this->outputFile);
		$this->assertEquals(100, $size[0]);
		$this->assertEquals(75, $size[1]);
	}

	public function testGetImageResizedUp() {
		$this->loadService('getImage.php?source=../test/resources/example.jpg&action=resize&width=1024&height=768', true);
		$size = getimagesize($this->outputFile);
		$this->assertEquals(1024, $size[0]);
		$this->assertEquals(768, $size[1]);
	}

	public function testGetPdf() {
		$this->loadService('getPdf.php?filePath=../test/resources/text.xml', true);
		$this->assertGreaterThan(100000, filesize($this->outputFile));
	}

	public function testGetPreview() {
		$code = 'phpunit_test_document';
		mkdir(ACTIVE_DOCUMENTS_DIRECTORY.$code);
		$filePath = ACTIVE_DOCUMENTS_DIRECTORY.$code.'/.structure.xml';
		/*
		if (!is_file($filePath)) {
			if(!copy(__DIR__.'/resources/colorboxes.xml', $filePath)) {
				$this->markTestSkipped('Cannot copy file!');
				return;
			}
		}
		*/
		$this->loadService('getPreview.php?width=200&height=200&code='.$code, true);
		$size = getimagesize($this->outputFile);
		$this->assertEquals(200, $size[0]);
		$this->assertEquals(150, $size[1]);
		/*
		unlink($filePath);
		rmdir(ACTIVE_DOCUMENTS_DIRECTORY.$code);
		*/
	}

	public function testGetImageAttributes() {
		$result = $this->loadService('getImageAttributes.php?file=../test/resources/example.jpg');
		$this->assertEquals('<result width="1000" height="750" format="JPEG" colorspace="RGB" />', $result);
	}

	public function testLogin() {
		$result = $this->loadService('login.php');
		$this->assertStringStartsWith('<result name="Default" login="default" rights="', $result);
	}

	public function testGetDirectory() {
		$result = $this->loadService('getDirectoryStructure.php');
		$this->assertContains('<directoryStructure>', $result);
		$this->assertContains('<entry name="', $result);
	}

	public function testListDirectory() {
		$result = $this->loadService('getDirectoryStructure.php');
		preg_match('/<entry[^>]*label="public[^>]*path="([^"]*)"/', $result, $matches);
		if (empty($matches[1])) {
			preg_match('/<entry[^>]*label="format_[^>]*path="([^"]*)"/', $result, $matches);
		}
		$result = $this->loadService('getDirectoryList.php?path='.$matches[1]);
		$this->assertContains('entry type="document"', $result);
	}

	public function testGetDocument() {
		$result = $this->loadService('getDirectoryStructure.php');
		preg_match('/<entry[^>]*label="public[^>]*path="([^"]*)"/', $result, $matches);
		if (empty($matches[1])) {
			preg_match('/<entry[^>]*label="format_[^>]*path="([^"]*)"/', $result, $matches);
		}
		$result = $this->loadService('getDirectoryList.php?path='.$matches[1]);
		preg_match('/documentFilePath="([^"]*)"/', $result, $matches);
		$result = $this->loadService('getDocument.php?filePath='.$matches[1]);
		$this->assertContains('<document', $result);
	}

	public function testFilter() {
		$result = $this->loadService('getDirectoryStructure.php?reload=1');
		$hasFilter1 = (strpos($result, 'name="format_x1"') !== false);
		$hasFilter2 = (strpos($result, 'name="format_x2"') !== false);
		$result = $this->loadService('getDirectoryStructure.php?filterPrefix=format_&filterSuffix=x1');
		if ($hasFilter1 && $hasFilter2) {
			$this->assertFalse(strpos($result, 'name="format_x2"'));
			$this->assertFalse(strpos($result, 'name="format_x1"'));
		}
	}

	private function loadService($url, $saveToFile = false) {
		if (shell_exec('which wget')) {
			$command = "wget -q '".$this->servicesUrl.$url."' -O ".$this->outputFile;
			shell_exec($command);
			if (!$saveToFile) {
				return file_get_contents($this->outputFile);
			}
		} else if (shell_exec('which curl')) {
			$command = "curl '".$this->servicesUrl.$url."'";
			if ($saveToFile) {
				$command .= ' > '.$this->outputFile;
			}
			return shell_exec($command);
		} else {
			$this->markTestSkipped(
              'No wget or curl available!'
            );
		}
	}
}