/** * @param $id * @return Document * @throws NotFoundHttpException */ protected function findDocument($id) { if (($model = Document::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('Документ не найден.'); } }
* Created by PhpStorm. * User: bill * Date: 26.01.15 * Time: 11:31 */ use app\tests\codeception\_pages\mobile\number\CreatePage; use app\modules\mobile\models\Number; use app\modules\mobile\models\Document; use app\modules\directory\models\Employee; $I = new FunctionalTester($scenario); $I->wantTo("ensure that creation Number model works"); $owner = $I->grabRecord(Employee::className(), ['id' => 1]); $newDocument = 'file1.jpg'; $number = '9876543210'; $I->amGoingTo("prepare database."); Document::deleteAll(['filename' => $newDocument]); Number::deleteAll(['number' => $number]); $page = CreatePage::openBy($I); $I->dontSeeElement("input[type=file]"); $I->dontSeeElement("#documents-panel-body"); $I->dontSeeElement("#history-panel-body"); $page->submit(['number' => $number, 'ownerName' => $owner->fullName, 'ownerPost' => $owner->post, 'operatorId' => 'operator2', 'destination' => Number::DESTINATION_MODEM, 'limit' => "1000", 'options' => [Number::OPTION_ACCOUNTING, Number::OPTION_DIRECTORY], 'comment' => 'This is comment of testing number.']); $I->SeeRecord(Number::className(), ['number' => $number, 'ownerId' => 1, 'operatorId' => 'operator2', 'destination' => Number::DESTINATION_MODEM, 'limit' => 1000, 'options' => [Number::OPTION_ACCOUNTING, Number::OPTION_DIRECTORY], 'history' => [['ownerId' => 'employee1', 'rentDate' => time()]], 'comment' => 'This is comment of testing number.']); $I->see("Номер успешно создан", '.alert-success'); $I->seeInField($page->fieldSelector('ownerName'), $owner->fullName); $I->seeInField($page->fieldSelector('ownerPost'), $owner->posts[1]); $I->seeOptionIsSelected($page->fieldSelector('operatorId'), "Оператор 2"); $I->seeOptionIsSelected($page->fieldSelector('destination'), Number::DESTINATION_MODEM); $I->seeCheckboxIsChecked($page->fieldSelector(Number::OPTION_ACCOUNTING)); $I->seeCheckboxIsChecked($page->fieldSelector(Number::OPTION_DIRECTORY)); $I->dontSeeCheckboxIsChecked($page->fieldSelector(Number::OPTION_TRIP));
/** * @param UploadedFile|string $file * @throws \InvalidArgumentException|\RuntimeException * @return bool */ public function attachDocument($file) { $document = new Document(); if ($file instanceof UploadedFile) { $document->filename = $file->name; $document->contentType = $file->type; } elseif (is_file($file)) { $document->filename = basename($file); $document->contentType = mime_content_type($file); } else { throw new \InvalidArgumentException(__METHOD__ . ": unable attach document '{$file}'.'"); } $document->ownerId = $this->getPrimaryKey(); $document->file = $file; if ($document->save()) { return true; } else { throw new \RuntimeException(__METHOD__ . ": unable attach document '{$document->filename}': " . implode(", ", array_keys($document->getErrors())) . " is invalid"); } }
/** * Created by PhpStorm. * User: bill * Date: 13.02.15 * Time: 16:15 */ use yii\helpers\Html; use app\modules\mobile\models\Document; use yii\data\ActiveDataProvider; use yii\widgets\ListView; /* @var $this yii\web\View */ /* @var $ownerId MongoId|string */ ?> <?php $dataProvider = new ActiveDataProvider(['query' => Document::find()->select(['filename'])->where(['ownerId' => $ownerId]), 'pagination' => false, 'sort' => false]); ?> <?php echo ListView::widget(['dataProvider' => $dataProvider, 'options' => ['class' => 'list-group'], 'summary' => "", 'itemOptions' => ['class' => 'list-group-item'], 'itemView' => function ($model) { /** @var $model Document */ return Html::a($model->filename, ['render-document', 'id' => (string) $model->getPrimaryKey()], ['target' => '_blank']) . Html::a("<span class='glyphicon glyphicon-trash'></span>", ['detach-document', 'id' => (string) $model->getPrimaryKey()], ['style' => 'float: right', 'class' => 'detach-document']); }]); ?> <?php $script = <<<JS \$('a.detach-document').on('click', function(e) { e.preventDefault(); \$('#documents-panel-body').load(\$(this).attr('href')); });
<?php /** * Created by PhpStorm. * User: bill * Date: 16.02.15 * Time: 16:06 */ use app\modules\mobile\models\Document; Document::deleteAll(); $I = new FunctionalTester($scenario); $I->wantTo('ensure that remove document works'); $id = Document::getCollection()->insertFile(\Yii::getAlias('@data') . '/file1.jpg'); $I->seeRecord(Document::className(), ['_id' => $id]); $I->amOnPage(['mobile/number/detach-document', 'id' => (string) $id]); $I->dontSeeRecord(Document::className(), ['_id' => $id]);
public function testGetDocuments() { Document::deleteAll(); $this->specify("Attachment must be exist", function () { $numberId = $this->tester->haveRecord(Number::className(), ['number' => '9999999999']); $model = $this->tester->grabRecord(Number::className(), ['_id' => $numberId]); $model->attachDocument(\Yii::getAlias('@data') . '/file1.jpg'); $model->attachDocument(\Yii::getAlias('@data') . '/file2.pdf'); expect(count($model->documents))->equals(2); foreach ($model->documents as $document) { expect($document instanceof Document)->true(); expect($document->ownerId)->equals($model->getPrimaryKey()); } }); }
public function _beforeSuite($settings = array()) { Document::deleteAll(); $this->loadFixtures(); }
public function actionDocuments() { $this->stdout("Добавляю документы\n", Console::FG_BLUE, Console::BOLD); $this->stdout("Источник: {$this->documentsPath}\n"); $this->stdout("Очищаю хранилище документов\n", Console::FG_RED, Console::BOLD); Document::deleteAll([]); $aborted = ['dirs' => [], 'files' => []]; $success = 0; foreach (scandir($this->documentsPath) as $dirName) { $directory = $this->documentsPath . '/' . $dirName; if (is_dir($directory)) { if ($number = Number::findOne(['number' => $dirName])) { foreach (scandir($directory) as $fileName) { $file = $directory . '/' . $fileName; if (is_file($file)) { if (substr($fileName, 0, 1) !== '.' && $number->attachDocument($file)) { $success++; } else { $aborted['files'][] = $file; } } } } else { $aborted['dirs'][] = $dirName; } } } $this->stdout("Успешно добавлено {$success} документов\n", Console::FG_GREEN, Console::BOLD); $this->stdout("Каталоги для которых не найдены номера:\n"); foreach ($aborted['dirs'] as $dir) { $this->stdout("{$dir}\n"); } $this->stdout("Файлы которые не удалось добавить:\n"); foreach ($aborted['files'] as $file) { $this->stdout("{$file}\n"); } return Controller::EXIT_CODE_NORMAL; }