FileListContext.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2018, Daniel Calviño Sánchez (danxuliu@gmail.com)
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. use Behat\Behat\Context\Context;
  23. class FileListContext implements Context, ActorAwareInterface {
  24. /**
  25. * @var Actor
  26. */
  27. private $actor;
  28. /**
  29. * @var array
  30. */
  31. private $fileListAncestorsByActor;
  32. /**
  33. * @var Locator
  34. */
  35. private $fileListAncestor;
  36. /**
  37. * @BeforeScenario
  38. */
  39. public function initializeFileListAncestors() {
  40. $this->fileListAncestorsByActor = array();
  41. $this->fileListAncestor = null;
  42. }
  43. /**
  44. * @param Actor $actor
  45. */
  46. public function setCurrentActor(Actor $actor) {
  47. $this->actor = $actor;
  48. if (array_key_exists($actor->getName(), $this->fileListAncestorsByActor)) {
  49. $this->fileListAncestor = $this->fileListAncestorsByActor[$actor->getName()];
  50. } else {
  51. $this->fileListAncestor = null;
  52. }
  53. }
  54. /**
  55. * Sets the file list ancestor to be used in the steps performed by the
  56. * given actor from that point on (until changed again).
  57. *
  58. * This is meant to be called from other contexts, for example, when the
  59. * Files app or the public page for a shared folder are opened.
  60. *
  61. * The FileListAncestorSetter trait can be used to reduce the boilerplate
  62. * needed to set the file list ancestor from other contexts.
  63. *
  64. * @param null|Locator $fileListAncestor the file list ancestor
  65. * @param Actor $actor the actor
  66. */
  67. public function setFileListAncestorForActor($fileListAncestor, Actor $actor) {
  68. $this->fileListAncestorsByActor[$actor->getName()] = $fileListAncestor;
  69. }
  70. /**
  71. * @return Locator
  72. */
  73. public static function mainWorkingIcon($fileListAncestor) {
  74. return Locator::forThe()->css(".mask.icon-loading")->
  75. descendantOf($fileListAncestor)->
  76. describedAs("Main working icon in file list");
  77. }
  78. /**
  79. * @return Locator
  80. */
  81. public static function breadcrumbs($fileListAncestor) {
  82. return Locator::forThe()->css("#controls .breadcrumb")->
  83. descendantOf($fileListAncestor)->
  84. describedAs("Breadcrumbs in file list");
  85. }
  86. /**
  87. * @return Locator
  88. */
  89. public static function createMenuButton($fileListAncestor) {
  90. return Locator::forThe()->css("#controls .button.new")->
  91. descendantOf($fileListAncestor)->
  92. describedAs("Create menu button in file list");
  93. }
  94. /**
  95. * @return Locator
  96. */
  97. private static function createMenuItemFor($fileListAncestor, $newType) {
  98. return Locator::forThe()->xpath("//div[contains(concat(' ', normalize-space(@class), ' '), ' newFileMenu ')]//span[normalize-space() = '$newType']/ancestor::li")->
  99. descendantOf($fileListAncestor)->
  100. describedAs("Create $newType menu item in file list");
  101. }
  102. /**
  103. * @return Locator
  104. */
  105. public static function createNewFolderMenuItem($fileListAncestor) {
  106. return self::createMenuItemFor($fileListAncestor, "New folder");
  107. }
  108. /**
  109. * @return Locator
  110. */
  111. public static function createNewFolderMenuItemNameInput($fileListAncestor) {
  112. return Locator::forThe()->css(".filenameform input")->
  113. descendantOf(self::createNewFolderMenuItem($fileListAncestor))->
  114. describedAs("Name input in create new folder menu item in file list");
  115. }
  116. /**
  117. * @return Locator
  118. */
  119. public static function rowForFile($fileListAncestor, $fileName) {
  120. return Locator::forThe()->xpath("//*[@id = 'fileList']//span[contains(concat(' ', normalize-space(@class), ' '), ' nametext ') and normalize-space() = '$fileName']/ancestor::tr")->
  121. descendantOf($fileListAncestor)->
  122. describedAs("Row for file $fileName in file list");
  123. }
  124. /**
  125. * @return Locator
  126. */
  127. public static function rowForFilePreceding($fileListAncestor, $fileName1, $fileName2) {
  128. return Locator::forThe()->xpath("//preceding-sibling::tr//span[contains(concat(' ', normalize-space(@class), ' '), ' nametext ') and normalize-space() = '$fileName1']/ancestor::tr")->
  129. descendantOf(self::rowForFile($fileListAncestor, $fileName2))->
  130. describedAs("Row for file $fileName1 preceding $fileName2 in file list");
  131. }
  132. /**
  133. * @return Locator
  134. */
  135. public static function favoriteMarkForFile($fileListAncestor, $fileName) {
  136. return Locator::forThe()->css(".favorite-mark")->
  137. descendantOf(self::rowForFile($fileListAncestor, $fileName))->
  138. describedAs("Favorite mark for file $fileName in file list");
  139. }
  140. /**
  141. * @return Locator
  142. */
  143. public static function notFavoritedStateIconForFile($fileListAncestor, $fileName) {
  144. return Locator::forThe()->css(".icon-star")->
  145. descendantOf(self::favoriteMarkForFile($fileListAncestor, $fileName))->
  146. describedAs("Not favorited state icon for file $fileName in file list");
  147. }
  148. /**
  149. * @return Locator
  150. */
  151. public static function favoritedStateIconForFile($fileListAncestor, $fileName) {
  152. return Locator::forThe()->css(".icon-starred")->
  153. descendantOf(self::favoriteMarkForFile($fileListAncestor, $fileName))->
  154. describedAs("Favorited state icon for file $fileName in file list");
  155. }
  156. /**
  157. * @return Locator
  158. */
  159. public static function mainLinkForFile($fileListAncestor, $fileName) {
  160. return Locator::forThe()->css(".name")->
  161. descendantOf(self::rowForFile($fileListAncestor, $fileName))->
  162. describedAs("Main link for file $fileName in file list");
  163. }
  164. /**
  165. * @return Locator
  166. */
  167. public static function renameInputForFile($fileListAncestor, $fileName) {
  168. return Locator::forThe()->css("input.filename")->
  169. descendantOf(self::rowForFile($fileListAncestor, $fileName))->
  170. describedAs("Rename input for file $fileName in file list");
  171. }
  172. /**
  173. * @return Locator
  174. */
  175. public static function commentActionForFile($fileListAncestor, $fileName) {
  176. return Locator::forThe()->css(".action-comment")->
  177. descendantOf(self::rowForFile($fileListAncestor, $fileName))->
  178. describedAs("Comment action for file $fileName in file list");
  179. }
  180. /**
  181. * @return Locator
  182. */
  183. public static function shareActionForFile($fileListAncestor, $fileName) {
  184. return Locator::forThe()->css(".action-share")->
  185. descendantOf(self::rowForFile($fileListAncestor, $fileName))->
  186. describedAs("Share action for file $fileName in file list");
  187. }
  188. /**
  189. * @return Locator
  190. */
  191. public static function fileActionsMenuButtonForFile($fileListAncestor, $fileName) {
  192. return Locator::forThe()->css(".action-menu")->
  193. descendantOf(self::rowForFile($fileListAncestor, $fileName))->
  194. describedAs("File actions menu button for file $fileName in file list");
  195. }
  196. /**
  197. * @return Locator
  198. */
  199. public static function fileActionsMenu() {
  200. return Locator::forThe()->css(".fileActionsMenu")->
  201. describedAs("File actions menu in file list");
  202. }
  203. /**
  204. * @return Locator
  205. */
  206. private static function fileActionsMenuItemFor($itemText) {
  207. return Locator::forThe()->xpath("//a[normalize-space() = '$itemText']")->
  208. descendantOf(self::fileActionsMenu())->
  209. describedAs($itemText . " item in file actions menu in file list");
  210. }
  211. /**
  212. * @return Locator
  213. */
  214. public static function addToFavoritesMenuItem() {
  215. return self::fileActionsMenuItemFor("Add to favorites");
  216. }
  217. /**
  218. * @return Locator
  219. */
  220. public static function removeFromFavoritesMenuItem() {
  221. return self::fileActionsMenuItemFor("Remove from favorites");
  222. }
  223. /**
  224. * @return Locator
  225. */
  226. public static function detailsMenuItem() {
  227. return self::fileActionsMenuItemFor("Details");
  228. }
  229. /**
  230. * @return Locator
  231. */
  232. public static function renameMenuItem() {
  233. return self::fileActionsMenuItemFor("Rename");
  234. }
  235. /**
  236. * @return Locator
  237. */
  238. public static function viewFileInFolderMenuItem() {
  239. return self::fileActionsMenuItemFor("View in folder");
  240. }
  241. /**
  242. * @return Locator
  243. */
  244. public static function deleteMenuItem() {
  245. return self::fileActionsMenuItemFor("Delete");
  246. }
  247. /**
  248. * @Given I create a new folder named :folderName
  249. */
  250. public function iCreateANewFolderNamed($folderName) {
  251. $this->actor->find(self::createMenuButton($this->fileListAncestor), 10)->click();
  252. $this->actor->find(self::createNewFolderMenuItem($this->fileListAncestor), 2)->click();
  253. $this->actor->find(self::createNewFolderMenuItemNameInput($this->fileListAncestor), 2)->setValue($folderName . "\r");
  254. }
  255. /**
  256. * @Given I enter in the folder named :folderName
  257. */
  258. public function iEnterInTheFolderNamed($folderName) {
  259. $this->actor->find(self::mainLinkForFile($this->fileListAncestor, $folderName), 10)->click();
  260. }
  261. /**
  262. * @Given I open the details view for :fileName
  263. */
  264. public function iOpenTheDetailsViewFor($fileName) {
  265. $this->actor->find(self::fileActionsMenuButtonForFile($this->fileListAncestor, $fileName), 10)->click();
  266. $this->actor->find(self::detailsMenuItem(), 2)->click();
  267. }
  268. /**
  269. * @Given I rename :fileName1 to :fileName2
  270. */
  271. public function iRenameTo($fileName1, $fileName2) {
  272. $this->actor->find(self::fileActionsMenuButtonForFile($this->fileListAncestor, $fileName1), 10)->click();
  273. $this->actor->find(self::renameMenuItem(), 2)->click();
  274. // For reference, due to a bug in the Firefox driver of Selenium and/or
  275. // maybe in Firefox itself, as a range is selected in the rename input
  276. // (the name of the file, without its extension) when the value is set
  277. // the window must be in the foreground. Otherwise, if the window is in
  278. // the background, instead of setting the value in the whole field it
  279. // would be set only in the selected range.
  280. // This should not be a problem, though, as the default behaviour is to
  281. // bring the browser window to the foreground when switching to a
  282. // different actor.
  283. $this->actor->find(self::renameInputForFile($this->fileListAncestor, $fileName1), 10)->setValue($fileName2 . "\r");
  284. }
  285. /**
  286. * @Given I mark :fileName as favorite
  287. */
  288. public function iMarkAsFavorite($fileName) {
  289. $this->iSeeThatIsNotMarkedAsFavorite($fileName);
  290. $this->actor->find(self::fileActionsMenuButtonForFile($this->fileListAncestor, $fileName), 10)->click();
  291. $this->actor->find(self::addToFavoritesMenuItem(), 2)->click();
  292. }
  293. /**
  294. * @Given I unmark :fileName as favorite
  295. */
  296. public function iUnmarkAsFavorite($fileName) {
  297. $this->iSeeThatIsMarkedAsFavorite($fileName);
  298. $this->actor->find(self::fileActionsMenuButtonForFile($this->fileListAncestor, $fileName), 10)->click();
  299. $this->actor->find(self::removeFromFavoritesMenuItem(), 2)->click();
  300. }
  301. /**
  302. * @When I view :fileName in folder
  303. */
  304. public function iViewInFolder($fileName) {
  305. $this->actor->find(self::fileActionsMenuButtonForFile($this->fileListAncestor, $fileName), 10)->click();
  306. $this->actor->find(self::viewFileInFolderMenuItem(), 2)->click();
  307. }
  308. /**
  309. * @When I delete :fileName
  310. */
  311. public function iDelete($fileName) {
  312. $this->actor->find(self::fileActionsMenuButtonForFile($this->fileListAncestor, $fileName), 10)->click();
  313. $this->actor->find(self::deleteMenuItem(), 2)->click();
  314. }
  315. /**
  316. * @When I open the unread comments for :fileName
  317. */
  318. public function iOpenTheUnreadCommentsFor($fileName) {
  319. $this->actor->find(self::commentActionForFile($this->fileListAncestor, $fileName), 10)->click();
  320. }
  321. /**
  322. * @Then I see that the file list is eventually loaded
  323. */
  324. public function iSeeThatTheFileListIsEventuallyLoaded() {
  325. if (!WaitFor::elementToBeEventuallyNotShown(
  326. $this->actor,
  327. self::mainWorkingIcon($this->fileListAncestor),
  328. $timeout = 10 * $this->actor->getFindTimeoutMultiplier())) {
  329. PHPUnit_Framework_Assert::fail("The main working icon for the file list is still shown after $timeout seconds");
  330. }
  331. }
  332. /**
  333. * @Then I see that the file list is currently in :path
  334. */
  335. public function iSeeThatTheFileListIsCurrentlyIn($path) {
  336. // The text of the breadcrumbs is the text of all the crumbs separated
  337. // by white spaces.
  338. PHPUnit_Framework_Assert::assertEquals(
  339. str_replace('/', ' ', $path), $this->actor->find(self::breadcrumbs($this->fileListAncestor), 10)->getText());
  340. }
  341. /**
  342. * @Then I see that it is not possible to create new files
  343. */
  344. public function iSeeThatItIsNotPossibleToCreateNewFiles() {
  345. // Once a file list is loaded the "Create" menu button is always in the
  346. // DOM, so it is checked if it is visible or not.
  347. PHPUnit_Framework_Assert::assertFalse($this->actor->find(self::createMenuButton($this->fileListAncestor))->isVisible());
  348. }
  349. /**
  350. * @Then I see that the file list contains a file named :fileName
  351. */
  352. public function iSeeThatTheFileListContainsAFileNamed($fileName) {
  353. PHPUnit_Framework_Assert::assertNotNull($this->actor->find(self::rowForFile($this->fileListAncestor, $fileName), 10));
  354. }
  355. /**
  356. * @Then I see that :fileName1 precedes :fileName2 in the file list
  357. */
  358. public function iSeeThatPrecedesInTheFileList($fileName1, $fileName2) {
  359. PHPUnit_Framework_Assert::assertNotNull($this->actor->find(self::rowForFilePreceding($this->fileListAncestor, $fileName1, $fileName2), 10));
  360. }
  361. /**
  362. * @Then I see that :fileName is marked as favorite
  363. */
  364. public function iSeeThatIsMarkedAsFavorite($fileName) {
  365. PHPUnit_Framework_Assert::assertNotNull($this->actor->find(self::favoritedStateIconForFile($this->fileListAncestor, $fileName), 10));
  366. }
  367. /**
  368. * @Then I see that :fileName is not marked as favorite
  369. */
  370. public function iSeeThatIsNotMarkedAsFavorite($fileName) {
  371. PHPUnit_Framework_Assert::assertNotNull($this->actor->find(self::notFavoritedStateIconForFile($this->fileListAncestor, $fileName), 10));
  372. }
  373. /**
  374. * @Then I see that :fileName has unread comments
  375. */
  376. public function iSeeThatHasUnreadComments($fileName) {
  377. PHPUnit_Framework_Assert::assertTrue($this->actor->find(self::commentActionForFile($this->fileListAncestor, $fileName), 10)->isVisible());
  378. }
  379. }