/** * POST /notes */ public function testCreate() { $created = new Note(); $created->setId(3); $expected = new Note(); $this->service->expects($this->once())->method('create')->with($this->equalTo($this->userId))->will($this->returnValue($created)); $this->service->expects($this->once())->method('update')->with(3, 'hi', $this->userId)->will($this->returnValue($expected)); $response = $this->controller->create('hi'); $this->assertEquals($expected, $response->getData()); $this->assertTrue($response instanceof DataResponse); }
public function testFromFile() { $file = $this->getMockBuilder('OCP\\Files\\File')->getMock(); $file->expects($this->any())->method('getId')->will($this->returnValue(3)); $file->expects($this->any())->method('getContent')->will($this->returnValue('content')); $file->expects($this->any())->method('getMTime')->will($this->returnValue(323)); $file->expects($this->any())->method('getName')->will($this->returnValue('file.txt')); $note = Note::fromFile($file); $this->assertEquals(3, $note->getId()); $this->assertEquals(323, $note->getModified()); $this->assertEquals('file', $note->getTitle()); $this->assertEquals('content', $note->getContent()); }
/** * Updates a note. Be sure to check the returned note since the title is * dynamically generated and filename conflicts are resolved * @param int $id the id of the note used to update * @param string $content the content which will be written into the note * @param string $handwrittenContent json content from the handwriting feature * the title is generated from the first line of the content * @throws NoteDoesNotExistException if note does not exist * @return \OCA\Notes\Db\Note the updated note */ public function update($id, $content, $userId, $handwrittenContent = "") { $folder = $this->getFolderForUser($userId); $file = $this->getFileById($folder, $id); $handwrittenFile = $this->getHandwrittenContent($id, $userId); $handwrittenFile->putContent($handwrittenContent); // generate content from the first line of the title $splitContent = explode("\n", $content); $title = $splitContent[0]; if (!$title) { $title = $this->l10n->t('New note'); } // prevent directory traversal $title = str_replace(array('/', '\\'), '', $title); // using a maximum of 100 chars should be enough $title = substr($title, 0, 100); // generate filename if there were collisions $currentFilePath = $file->getPath(); $basePath = '/' . $userId . '/files/Notes/'; $fileExtension = ".txt"; $newFilePath = $basePath . $this->generateFileName($folder, $title, $fileExtension, $id); // if the current path is not the new path, the file has to be renamed if ($currentFilePath !== $newFilePath) { $file->move($newFilePath); } $file->putContent($content); \OCP\Util::writeLog('notes', print_r(Note::fromFile($file), true), \OCP\Util::ERROR); return Note::fromFile($file, $handwrittenFile->getContent()); }
/** * POST /notes */ public function testCreate() { $content = 'yii'; $note = new Note(); $note->setId(4); $this->service->expects($this->once())->method('create')->with($this->equalTo($this->userId))->will($this->returnValue($note)); $this->service->expects($this->once())->method('update')->with($this->equalTo($note->getId()), $this->equalTo($content), $this->equalTo($this->userId))->will($this->returnValue($note)); $response = $this->controller->create($content); $this->assertEquals($note, $response->getData()); $this->assertTrue($response instanceof DataResponse); }