summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Utils/File/LocalFileUpload.php
blob: 60ed77d80654e89e97bf2256da3ca89d44f2de0f (plain)
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
<?php

namespace SMW\Tests\Utils\File;

use RuntimeException;
use SMW\Tests\Utils\Mock\MockSuperUser;
use UploadBase;

/**
 * @license GNU GPL v2+
 * @since 2.1
 *
 * @author mwjames
 */
class LocalFileUpload extends UploadBase {

	/**
	 * @var boolean
	 */
	private $removeTemporaryFile = true;

	/**
	 * @var string
	 */
	private $localUploadPath;

	/**
	 * @var string
	 */
	private $desiredDestName;

	/**
	 * @var DummyFileCreator
	 */
	private $dummyFileCreator;

	/**
	 * @var string
	 */
	private $error = '';

	/**
	 * @since 2.1
	 *
	 * @param string $localUploadPath
	 * @param string $desiredDestName
	 */
	public function __construct( $localUploadPath = '', $desiredDestName = '' ) {
		$this->localUploadPath = $localUploadPath;
		$this->desiredDestName = $desiredDestName;
	}

	/**
	 * @since 2.5
	 *
	 * @param DummyFileCreator $dummyFileCreator
	 */
	public function setDummyFileCreator( DummyFileCreator $dummyFileCreator ) {
		$this->dummyFileCreator = $dummyFileCreator;
	}

	/**
	 * @since 2.1
	 *
	 * @return string
	 */
	public function getUploadError() {
		return $this->error;
	}

	/**
	 * @since 2.1
	 */
	public function delete() {
		unlink( $this->localUploadPath );
	}

	/**
	 * @since 2.5
	 *
	 * @param string $localUploadPath
	 * @param string $desiredDestName
	 * @param string $pageText
	 * @param string $comment
	 *
	 * @return boolean
	 */
	public function doUploadCopyFromLocation( $localUploadPath, $desiredDestName, $pageText = '', $comment = '' ) {

		if ( !$this->dummyFileCreator instanceof DummyFileCreator ) {
			throw new RuntimeException( "Expected a DummyFileCreator instance." );
		}

		$this->dummyFileCreator->createFileWithCopyFrom(
			$desiredDestName,
			$localUploadPath
		);

		$this->doUploadFromLocation(
			$this->dummyFileCreator->getPath(),
			$desiredDestName,
			$pageText,
			$comment
		);
	}

	/**
	 * @since 2.5
	 *
	 * @param string $localUploadPath
	 * @param string $desiredDestName
	 * @param string $pageText
	 * @param string $comment
	 *
	 * @return boolean
	 */
	public function doUploadFromLocation( $localUploadPath, $desiredDestName, $pageText = '', $comment = '' ) {

		$localUploadPath = $this->createReadablePath( $localUploadPath );

		$this->initializePathInfo(
			$desiredDestName,
			$localUploadPath,
			filesize( $localUploadPath ),
			$this->removeTemporaryFile
		);

		$status = $this->performUpload(
			$comment,
			$pageText,
			false,
			new MockSuperUSer()
		);

		if ( !$status->isGood() ) {
			$this->error = $status->getWikiText();
			return false;
		}

		return true;
	}

	/**
	 * @since 2.1
	 *
	 * @param string $pageText
	 * @param string $comment
	 *
	 * @return boolean
	 */
	public function doUpload( $pageText = '', $comment = '' ) {
		return $this->doUploadFromLocation( $this->localUploadPath, $this->desiredDestName, $pageText, $comment );
	}

	/**
	 * @see UploadBase::initializeFromRequest
	 */
	public function initializeFromRequest( &$request ) {
	}

	/**
	 * @see UploadBase::getSourceType
	 */
	public function getSourceType() {
		return 'file';
	}

	private function createReadablePath( $path ) {

		$path = str_replace( [ '\\', '/' ], DIRECTORY_SEPARATOR, $path );

		if ( is_readable( $path ) ) {
			return $path;
		}

		throw new RuntimeException( "Expected an accessible {$path}" );
	}

}