public function testDelete() { $type = ProductRatingType::getNewInstance(Category::getRootNode()); $type->save(); $type2 = ProductRatingType::getNewInstance(Category::getRootNode()); $type2->save(); for ($k = 0; $k <= 1; $k++) { $review = ProductReview::getNewInstance($this->product, $this->user); $review->save(); $rating = ProductRating::getNewInstance($this->product, $type); $rating->rating->set(6 + $k); $rating->review->set($review); $rating->save(); $rating = ProductRating::getNewInstance($this->product, $type2); $rating->rating->set(4 + $k); $rating->review->set($review); $rating->save(); } $this->product->reload(); $this->assertEqual($this->product->ratingCount->get(), 4); $this->assertEqual($this->product->ratingSum->get(), 22); $this->assertEqual($this->product->rating->get(), 5.5); // delete last review $review->delete(); $this->product->reload(); $this->assertEqual($this->product->ratingCount->get(), 2); $this->assertEqual($this->product->ratingSum->get(), 10); $this->assertEqual($this->product->rating->get(), 5); // check rating summaries $summary = ProductRatingSummary::getInstance($this->product, $type2); $this->assertEqual($summary->rating->get(), 4); }
public function rate() { $product = Product::getInstanceByID($this->request->get('id'), Product::LOAD_DATA); $ratingTypes = ProductRatingType::getProductRatingTypes($product); $validator = $this->buildRatingValidator($ratingTypes->toArray(), $product, true); $redirect = new ActionRedirectResponse('product', 'index', array('id' => $product->getID())); if ($validator->isValid()) { $msg = $this->translate('_msg_rating_added'); if ($this->isAddingReview()) { $review = ProductReview::getNewInstance($product, $this->user); $review->loadRequestData($this->request); $review->ip->set($this->request->getIpLong()); // approval status $approval = $this->config->get('APPROVE_REVIEWS'); $review->isEnabled->set('APPROVE_REVIEWS_AUTO' == $approval || 'APPROVE_REVIEWS_USER' == $approval && !$this->user->isAnonymous()); $review->save(); $msg = $this->translate('_msg_review_added'); } foreach ($ratingTypes as $type) { $rating = ProductRating::getNewInstance($product, $type, $this->user); $rating->rating->set($this->request->get('rating_' . $type->getID())); if (isset($review)) { $rating->review->set($review); } $rating->ip->set($this->request->getIpLong()); $rating->save(); } if ($this->isAjax()) { $response = new JSONResponse(array('message' => $msg), 'success'); } else { $this->setMessage($msg); $response = $redirect; } $response->setCookie('rating_' . $product->getID(), true, strtotime('+' . $this->config->get('RATING_SAME_IP_TIME') . ' hours'), $this->router->getBaseDirFromUrl()); return $response; } else { if ($this->isAjax()) { return new JSONResponse(array('errors' => $validator->getErrorList())); } else { return $redirect; } } }
public function testRatingUpdate() { // initial rating $rating = ProductRating::getNewInstance($this->product); $rating->rating->set(6); $rating->save(); // change rating $rating->rating->set(8); $rating->save(); $this->product->reload(); $this->assertEqual($this->product->ratingCount->get(), 1); $this->assertEqual($this->product->ratingSum->get(), 8); $this->assertEqual($this->product->rating->get(), 8); }