diff --git a/server/controllers/ticket/create.php b/server/controllers/ticket/create.php index 46ddc615..bf840b8d 100644 --- a/server/controllers/ticket/create.php +++ b/server/controllers/ticket/create.php @@ -50,7 +50,6 @@ class CreateController extends Controller { $ticket = new Ticket(); $ticket->setProperties(array( - 'ticketNumber' => $ticket->generateUniqueTicketNumber(), 'title' => $this->title, 'content' => $this->content, 'language' => $this->language, diff --git a/server/models/DataStore.php b/server/models/DataStore.php index f4d85991..0248aaa9 100644 --- a/server/models/DataStore.php +++ b/server/models/DataStore.php @@ -17,7 +17,7 @@ abstract class DataStore { return ($bean) ? new static($bean) : new NullDataStore(); } public static function count() { - RedBean::count(static::TABLE); + return RedBean::count(static::TABLE); } private static function validateProp($propToValidate) { diff --git a/server/models/Ticket.php b/server/models/Ticket.php index e39a3beb..311df4b3 100644 --- a/server/models/Ticket.php +++ b/server/models/Ticket.php @@ -26,17 +26,17 @@ class Ticket extends DataStore { public static function getTicket($value, $property = 'id') { return parent::getDataStore($value, $property); } - + public function getDefaultProps() { return array( 'owner' => null, 'ticketNumber' => $this->generateUniqueTicketNumber() ); } - + public function store() { parent::store(); - + if ($this->author instanceof User) { $this->author->store(); } @@ -52,9 +52,9 @@ class Ticket extends DataStore { $firstTicketNumber = Ticket::getTicket(1)->ticketNumber; $gap = 176611; - $ticketNumber = ($firstTicketNumber - $minValue + $ticketQuantity * $gap) % $maxValue + $minValue; + $ticketNumber = ($firstTicketNumber - $minValue + $ticketQuantity * $gap) % ($maxValue - $minValue + 1) + $minValue; } return $ticketNumber; } -} \ No newline at end of file +} diff --git a/tests/ticket/create.rb b/tests/ticket/create.rb index e9209969..979895d9 100644 --- a/tests/ticket/create.rb +++ b/tests/ticket/create.rb @@ -108,8 +108,42 @@ describe '/ticket/create' do (ticket['closed']).should.equal('0') (ticket['department_id']).should.equal('1') (ticket['author_id']).should.equal($csrf_userid) + (ticket['ticket_number'].size).should.equal(6) ticket_user_relation = $database.getRow('ticket_user','1','ticket_id') (ticket_user_relation['user_id']).should.equal($csrf_userid) end + + it 'should set correct ticket number' do + result = request('/ticket/create',{ + title: 'Winter is coming1', + content: 'The north remembers', + departmentId: 1, + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) + result = request('/ticket/create',{ + title: 'Winter is coming2', + content: 'The north remembers', + departmentId: 1, + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) + result = request('/ticket/create',{ + title: 'Winter is coming3', + content: 'The north remembers', + departmentId: 1, + csrf_userid: $csrf_userid, + csrf_token: $csrf_token + }) + + ticket0 = $database.getRow('ticket','Winter is coming','title')['ticket_number'].to_i + ticket1 = $database.getRow('ticket','Winter is coming1','title')['ticket_number'].to_i + ticket2 = $database.getRow('ticket','Winter is coming2','title')['ticket_number'].to_i + ticket3 = $database.getRow('ticket','Winter is coming3','title')['ticket_number'].to_i + + (ticket1).should.equal((ticket0 - 100000 + 1 * 176611) % 900000 + 100000) + (ticket2).should.equal((ticket0 - 100000 + 2 * 176611) % 900000 + 100000) + (ticket3).should.equal((ticket0 - 100000 + 3 * 176611) % 900000 + 100000) + end end